Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regarding Primitive Data Types in C#

Tags:

c#

This MSDN article deals with Data Types.

It says:

For each primitive data type in Java, the core class library provides a wrapper class that represents it as a Java object. For example, the Int32 class wraps the int data type, and the Double class wraps the double data type.

On the other hand, all primitive data types in C# are objects in the System namespace. For each data type, a short name, or alias, is provided. For instance, int is the short name for System.Int32 and double is the short form of System.Double.

I ran to the Mono code to read the implementation of System.Int32 struct.

I found a few lines which have forced me to ask this question:

public const int MaxValue = 0x7fffffff;
public const int MinValue = -2147483648;

// This field is looked up by name in the runtime
internal int m_value;

I am assuming that MS would have implemented the struct in the same way. Is this any different from a wrapper? What exactly is the documentation trying to convey?

If MSDN is to be believed System.Int32 struct would be endlessly recursive and greatly confusing for me at least.

like image 892
msiyer Avatar asked Aug 02 '13 13:08

msiyer


1 Answers

Is this any different from a wrapper?

There are no wrapper types for the primitive types in C# like there are in Java, and the alias int for System.Int32 is not anything like a wrapper. It's just System.Int32 by a different name.

The primitive types in C# can be boxed, as they can in Java, but in C# when they are boxed they are boxed to System.Object whereas in Java they are boxed to their wrapper types.

What exactly is the documentation trying to convey?

That there is a difference between these two similar but different languages. At a naive glance, it would look like these things are the same:

Java: int -> Integer

C#: int -> System.Int32

But that is not the case at all. In Java, int is a primitive type, and Integer is a class that serves as a wrapper for int when ints need to be boxed. In C#, int and System.Int32 are exactly the same thing (a primitive type).

If MSDN is to be believed System.Int32 struct would be endlessly recursive and greatly confusing for me at least.

System.Int32 and int are exactly the same type in C#; int is merely a convenient alias for System.Int32. In Java, int and Integer are distinct. In C#, int and System.Int32 are not distinct.

So, to be very clear:

public const int MaxValue = 0x7fffffff;
public const int MinValue = -2147483648;

// This field is looked up by name in the runtime
internal int m_value;

can be rewritten to

public const System.Int32 MaxValue = 0x7fffffff;
public const System.Int32 MinValue = -2147483648;

// This field is looked up by name in the runtime
internal System.Int32 m_value;

and this does not change the meaning at all because int and System.Int32 are exactly the same thing. And then, you should refer to this previous question for the special handling of the built-in types.

like image 137
jason Avatar answered Oct 03 '22 10:10

jason