1

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 ?

0

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

6 Comments

To add to this, here are a list of all keywords: msdn.microsoft.com/en-us/library/x53a06bb.aspx
Ok thanks, but when they say value types inherit from System.ValueType, how can I see it ? I need something like : public something : System.ValueType
I'm just missing that int is an alias for System.Int32. Here's a complete list: msdn.microsoft.com/en-us/library/ya5y69ds.aspx
System.Int32 does not inherit from System.ValueType , does it ?
@Souregi: From this page: "Although ValueType is the implicit base class for value types, you cannot create a class that inherits from ValueType directly. Instead, individual compilers provide a language keyword or construct (such as struct in C# and Structure…End Structure in Visual Basic) to support the creation of value types."
|
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.