Click here to Skip to main content
15,884,085 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The concept of string interning establishes that each time a unique string literal is introduced in code, reference type String is instantiated and a reference to that instance is stored in the intern pool. However, anytime a string literal (that was already introduced in code) is introduced in code again, a reference (retrieved from the intern pool) to the already existing instance is returned. Thus, no new instance of type String is created and no new reference is stored in the intern pool. In relevance to the bottom example, in line 1, type String is instantiated and a reference to the instance (stored in the intern pool) is assgined to variable str1. In line 2, type String is NOT instanstiated. Instead a reference to the already existing instance of type String` is assigned variable str2.

string str1 = "Fun"; // line 1
string str2 = "Fun"; // line 2


What about when dealing with a literal value of Value type (e.g. a bool literal, an int literal, a char literal). For example, what if I introduce the same literal value of Value type (in this case of type Int32) in code more than once. Will the Value type (in this case type Int32) be instantiated each time the same literal value of Value type is introduced in code?

In other words, in line 1, value type Int32 is instantiated. The literal value (instance of type Int32) is stored directly in the variable num1. In line 2 however, is Value type Int32 instantiated again or not?

int num1 = 78; // line 1
int num2 = 78; // line 2


P.S. I am aware that a variable of Value type directly stores that value assigned to it (not a referenced to value assigned to it).

What I have tried:

I am aware there are Microsoft documentation related to these topics but I have not found anywhere this is explicitly answer. C# language.
Posted
Updated 28-Oct-21 21:35pm
Comments
BillWoodruff 29-Oct-21 6:25am    
once again you are posting a kind of semantic fantasia, where you pose as some kind of sophist disputing documentation.

if you want to learn C#, experiment, and observe behavior, and generate hypotheses, as well as study documentation.

imagine a carpenter that can tell you how a saw works, but, has never tried to use one.

1 solution

No in both cases.
The difference is that strings are reference types, int32 is a value type - and that's im portant, because neither of these lines strictly speaking instantiates anything - they create stack based variables and assign a value to them.

When you instantiate a reference type in a method
C#
void foo()
   {
   MyClass mc = new MyClass();
   }

The variable mc is also stack based, but the instance of the MyClass that is assigned is on the heap, and can be persisted beyond the method by assigning it to an external (class level) variable, or by returning it to the caller:
C#
MyClass MC;
void foo()
   {
   MyClass mc = new MyClass();
   ...
   MC = mc;
   }

C#
MyClass foo()
   {
   MyClass mc = new MyClass();
   ...
   return mc;
   }
in both cases, the variable mc ceases to exist when the method ends, but the heap based reference persists and can be used by other methods.

Value types are different: each time you use them, a copy is made so if we did this:
C#
int I;
void foo()
   {
   int i = 666;
   ...
   I = i;
   }
The value in both I and i would be the same - 666 - but they are not the same variable, so any change to one does not affect the other in any way:
C#
int I;
void foo()
   {
   int i = 666;
   ...
   I = i;
   i = i / 3;
   }
i and I now contain different values: 222 and 666 because they are value types.

So the answer to your question is "No" - because with value types no instantiation takes
place, so there is no interning either. Generally speaking, the "literal value" of a value type is stored directly in the IL code which constructs and initializes the variable.

And strings are a special case: they intern because they are immutable, not because they are reference types - no other reference type interns!
 
Share this answer
 
Comments
BillWoodruff 29-Oct-21 4:38am    
i think this post might be from the latest incarnation of someone we know, whose specialty is semantic confusion. New member, #1 post.

What String is vis-à-vis reference/value-type, and how Strings are managed vis-à-vis heap/stack has some complexity that Eric Lippert has written about. Reference on request.
OriginalGriff 29-Oct-21 5:14am    
I figured that myself. But ... if he has learned from his previous incarnations and remains civil, I'm happy to leave him be, and even answer.

Strings are complicated beasties! I try not to introduce too much confusion in a single post - that is what articles are for :laugh:
BillWoodruff 29-Oct-21 6:19am    
You're kinder than i am :) Indeed, strings are weird critters, and i hear echoes of Eric's classic mantra "implementation detail" every time i wander into the wilderness of what a char[] is doing in a box like 'string.
Chris_PBacon 29-Oct-21 17:08pm    
Sorry I missed the conversation Griff and Bill.

Griff I have thoroughly read your solution. And I 100% AGREE with you on the stack and heap details you gave. However, I 100% DISAGREE with you when you said instantiation of a type (Reference type or Value type) does not occur when a literal value, of Reference type (e.g. String) or Value type (e.g. Int32) is present in code.

C# documentation https://docs.microsoft.com/en-us/dotnet/api/system.string?view=net-5.0 states "You can instantiate a String object by assigning a string literal to a String variable".

C# documentation https://docs.microsoft.com/en-us/dotnet/api/system.int32?view=net-5.0 states "You can instantiate an Int32 value by declaring an Int32 variable and assign it a literal integer value that is within the range of the Int32 data type"

Even so, a reference to a literal value of Reference type or a literal value of Value type doesn't even have to be assigned to a variable for the type (the literal value corresponds to) to be instantiated.

For example: "Fun".GetType(); and 78.GetType();
will result in no compile time error being generated. Method GetType in both examples will return the type of the current instance as stated in https://docs.microsoft.com/en-us/dotnet/api/system.object.gettype?view=net-5.0. And as you instance methods are invoked via an instance of a type. Thus, a literal value of Reference type or Value type is indeed instance to the type it corresponds to.

Maybe I am wrong. But the spec does not lie and neither does the C# code lie.
Chris_PBacon 30-Oct-21 19:10pm    
Well I was hoping we would dance a little bit on the topic of whether a literal value is considered an instance of the type it corresponds to XD

By you not replying back I will take that as you agreeing with my argument. Wink wink ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900