最佳答案
In C#, Structs are managed in terms of values, and objects are in reference. From my understanding, when creating an instance of a class, the keyword new
causes C# to use the class information to make the instance, as in below:
class MyClass
{
...
}
MyClass mc = new MyClass();
For struct, you're not creating an object but simply set a variable to a value:
struct MyStruct
{
public string name;
}
MyStruct ms;
//MyStruct ms = new MyStruct();
ms.name = "donkey";
What I do not understand is if declare variables by MyStruct ms = new MyStruct()
, what is the keyword new
here is doing to the statement? . If struct cannot be an object, what is the new
here instantiating?