简体   繁体   中英

Where does “struct” come from?

I am trying to understand more deeply values type and System.ValueType

My problem is when I right click on int and I click to "go to definition" I go to System.Int32 located in the mscorlib.dll

Now when I right click on struct or (enum) and I click to "go to definition", there is an error displayed : "Cannot navigate to 'struct'"

Where does "struct" or "enum" come from ?

System.Int32 is a type.

Both struct and enum are keywords rather than types.

As far as System.ValueType is concerned, take a look at the documentation:

MSDN - ValueType Class (System)

It explicitly states that you can't create a class that inherits from ValueType . Instead, you have to use one of the types that implicitly inherits from the class using keywords such as struct and enum .

int is an alias for the System.Int32 type, so it has a Type Definition that you can navigate to.

struct and enum do not correspond to types; they are merely keywords in the C# language.

Interesting note under the hood, enum refers to System.Enum , and all enum types derive from this. However, the usage is so specialized, the compiler will not allow you to derive from it directly.

A struct type passes by value instead of reference - like passing a non-pointer variable in C#. The nuances of when to use a struct or class is fairly complex if you don't understand pointers, but basically all value passes are copies, not references to thew original copy.

An enum is a class which contains nor more than a finite list of entries or flags, and has a few helper methods from the aformentioned System.Emum . This is most useful for things like

enum direction { Up, Down, Left, Right }

Otherwise, see Classes and Structs and Enumeration Types .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM