Mehdi Souregi
Mehdi Souregi

Reputation: 3265

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 ?

Upvotes: 1

Views: 119

Answers (3)

Justin Niessner
Justin Niessner

Reputation: 245419

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.

Upvotes: 7

David
David

Reputation: 10708

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.

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180788

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.

Upvotes: 7

Related Questions