Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to know/check: Int32 inherits from ValueType, ValueType inherits from Object?

I cannot find the relationships between these types using .Net reflector. Any idea?

like image 410
user1033098 Avatar asked Aug 02 '12 06:08

user1033098


People also ask

Are value types derived from system object?

While value types are stored generally in the stack, reference types are stored in the managed heap. A value type derives from System. ValueType and contains the data inside its own memory allocation. In other words, variables or objects or value types have their own copy of the data.

Is System Int32 a Value type in c#?

Int32 is an immutable value type that represents signed integers with values that range from negative 2,147,483,648 (which is represented by the Int32. MinValue constant) through positive 2,147,483,647 (which is represented by the Int32. MaxValue constant. .

Is Class A value type C#?

In C#, classes are not value types.


1 Answers

Since you say "using .Net reflector":

enter image description here

If you wanted reflection:

Type type = typeof (int);
while(type != null)
{
    Console.WriteLine(type.FullName);
    type = type.BaseType;
}

which shows:

System.Int32
System.ValueType
System.Object

and if you mean the IL:

.class public sequential ansi serializable sealed beforefieldinit Int32
    extends System.ValueType

and:

.class public abstract auto ansi serializable beforefieldinit ValueType
    extends System.Object

(in reflector, select the type's node, and select IL as the view)

If you mean the C# view, then:

public struct Int32 ...

is enough; the struct keyword means: inherits from ValueType (although not quite in the usual C# class way). ValueType remains a regular class, and has:

public abstract class ValueType ...

and as usual, a class which doesn't specify a base-type means: inherits from object.

like image 119
Marc Gravell Avatar answered Oct 25 '22 06:10

Marc Gravell