The differences between struct and class:
1, struct is value type, allocate space from stack. And class is reference type, allocate space from heap.
2, struct cannot inherit from the other struct or class, only can inherit interfaces. And struct can't be inherited because it is implicit sealed. But class can do them all. Also because the struct is sealed then all its members can't be protected.
3, struct can't have parameterless constructor, but class can
4, the default modifier of struct is public but for class it is private.
5, in struct there is no initializer, but class can. See code below.
1, struct is value type, allocate space from stack. And class is reference type, allocate space from heap.
2, struct cannot inherit from the other struct or class, only can inherit interfaces. And struct can't be inherited because it is implicit sealed. But class can do them all. Also because the struct is sealed then all its members can't be protected.
3, struct can't have parameterless constructor, but class can
4, the default modifier of struct is public but for class it is private.
5, in struct there is no initializer, but class can. See code below.
public struct myStruct
{
public myStruct(){}; //compile error
public string s = "abc"; //compile error
}
{
public myStruct(){}; //compile error
public string s = "abc"; //compile error
}
No comments:
Post a Comment