DEV Community

SelfTuaght
SelfTuaght

Posted on

C# value type variables and reference type variables (Question)

Question: why is the new operator not used when initializing predefined class type variables(such a variable of type string) or predefined struct type variables(such as a variable of type int)?
For example:
string name = "Richard";
int number = 36;

The new operator was never used when assigning such values to variables name and number. But are such values still considered instances of the respective predefined type.

Reason why I am asking this is because if I were to define a custom class type(say custom class type Person) or custom struct type(say custom struct type Dog). And if I were to declare a variable of such custom class type and such custom struct type, the value assigned to such variables would be an instance of their types created via the use of the new operator.
For example:
Person person1 = new Person( );
Dog dog1 = new Dog( );

Thus, is string type value(string literal) Richard an instance of predefined class type String(alias: string) just like how new Person( ) is an instance of user-defined class type Person?
And is in type value(integral literal) 24 an instance of predefined struct type Int32(alias: int) just like how new Dog( ) is an instance of user-defined struct type Dog?

Leave you comments down below answer the above.

Top comments (3)

Collapse
 
jayjeckel profile image
Jay Jeckel

Yes, the string literal "Richard" has the type String and 24 literal has the type Int32.

The new keyword is how we tell the compiler to instance arbitrary types when we need them. Literals are similar, when you type "Richard" you are telling the compiler to instance a String type object with the given content and 24 is telling the compiler to instance a Int32 object with the value of twenty-four.

You can instance string objects yourself. For example, one string constructor takes a length and a char to fill that length, eg new string('=', 10) instances a string object with content the same as the literal "==========".

I don't think the number types have useful constructors, but you can manually instance them if you want, ie int num = new Int32(). That will give you a variable that is equal to the default value of the type, which in this case would be zero.

Lastly, you can find out the type of any object, literal or otherwise, using the GetType() method, such as Console.WriteLine("Richard".GetType()); which would write System.String to the console.

Collapse
 
selftuaght profile image
SelfTuaght • Edited

Hey man, thanks for the input but can you link any web sources that support the following statement you made: "Literals are similar, when you type "Richard" you are telling the compiler to instance a String type object with the given content and 24 is telling the compiler to instance a Int32 object with the value of twenty-four."

Is there any C# Microsoft documentation that supports such argument?

Collapse
 
jayjeckel profile image
Jay Jeckel

No, I can't think of a link that directly says this. It is kind of assumed that one would understand that a string literal is a string object. If you need proof, as I said in my post, Console.WriteLine("Richard".GetType()); will show that a string literal is of the type System.String and Console.WriteLine(24.GetType()); will show a integer literal is of type System.Int32.

If you have to have a link, I guess the Strings page of the C# Programming Guide implies it by showing a string variable being initialized with a string literal:

// Initialize with a regular string literal.
string oldPath = "c:\\Program Files\\Microsoft Visual Studio 8.0";
Enter fullscreen mode Exit fullscreen mode

Likewise, the Integral Numeric Types page of the C# reference makes it about as clear as can be that numeric literals are of the Int32 type unless they have a literal suffix that declares them as another type.

int a = 123;
System.Int32 b = 123;
Enter fullscreen mode Exit fullscreen mode

Some comments may only be visible to logged-in visitors. Sign in to view all comments.