Is the value assigned to a struct type variable or class type variable(user-defined or predefined) always an instance of the type? [closed]

家住魔仙堡 提交于 2021-01-16 04:30:35

问题


Been programming for a while(C#). However, as I have been getting more in depth concerning the Stack and Heap, the concept of instances of a class type(user-defined and/or predefined) and a struct type(user-defined and/or predefined) still confuses me. This is a long post but help is greatly appreciated. Thanks.


In the bottom example, an instance of class type Person(a user-defined class type) is created and assigned(technically, the memory location of where the instance/object is found in the Heap, once it has been created of course) to variable person1 of class type Person:

Person person1 = new Person( );

However, in the bottom example a String type(alias: string. A predefined class type) value is assigned to variable name of class type String(alias: string):

string name = "Richard"

Question #1: Is Richard an instance of predefined class type String(alias: string)? If so how(because no where is the new operator used)? If not what is it actually(just a value)? What goes on behind the scenes with this value? Is it converted into an instance of type String behind the scenes?

Question #2: Also are both the instance of predefined class type Person(new Person( )) and the value of type String(Richard) allocated on the Heap, not alongside the variable in which are assigned to? If so is this always the case for user-defined class types and predefined class types(provided by Microsoft)?

I have looked into the definition of predefined class type String(alias: string), specifically the definition of its constructors and can't see the connection between string type value Richard being an instance of class type String. If it even is an instance of class type String that is. The same can be said for user-defined struct types and predefined struct types.


In the bottom example, an instance of struct type Dog(a user-defined struct type) is created and assigned(the actual instance/object of the struct type Dog) to variable dog1 of struct type Dog:

Dog dog1 = new Dog( );

However, in the bottom example an int type(a predefined struct type) value is assigned to variable age of struct type Int32(alias: int):

int age = 24

Question #1: Is 24 an instance of predefined class type Int32(alias: int)? If so how? If not what is it actually(just a value)? What goes on behind the scenes with this value? Is it converted into an instance of type Int32(alias: int) behind the scenes?

Question #2: Also are both the instance of predefined struct type Dog(new Dog( )) and the value of type Int32(24) allocated on the Stack, alongside the variable in which they are assigned to? If so is this always the case for user-defined struct types and predefined struct types(provided by Microsoft)?

I have looked into the definition of predefined struct type Int32(alias: int), which has no explicit constructor declarations(thus, I'm guessing just the default parameter-less constructor implicitly declared in its definition) and can't see the connection between int type value *24 being an instance of struct type Int32. If it even is an instance of struct type Int32 that is.


Note, perhaps I stated something incorrect. Feel free to correct me. Still learning here.

来源:https://stackoverflow.com/questions/65728166/is-the-value-assigned-to-a-struct-type-variable-or-class-type-variableuser-defi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!