13

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.

msiyer
  • 821
  • 6
  • 20
  • The built-in types have aliases in C# allowing the same type to have distinct names. E.g. `int` and `System.Int32`. For more information see [Built-In Types Table (C# Reference)](http://msdn.microsoft.com/en-us/library/ya5y69ds.aspx). – Martin Liversage Aug 02 '13 at 14:26

2 Answers2

16

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.

Community
  • 1
  • 1
jason
  • 236,483
  • 35
  • 423
  • 525
  • What exactly is the "int" inside the implementation of System.Int32 struct? That is confusing me. – msiyer Aug 02 '13 at 13:50
  • 1
    It's exactly the same thing as `System.Int32`. `int` means `System.Int32`! – jason Aug 02 '13 at 13:51
  • 1
    @msiyer: IIRC, the "int" inside the "System.Int32" source code file is specially handled/compiled. It's a bit of magic soup to reference/use the core CLR type (that it is, but isn't, but is... it's _special_) In this case, the source code of core primitive types are kind of special that way and should only be used as reference. EDIT: See http://stackoverflow.com/questions/16113850/if-int32-is-just-an-alias-for-int-how-can-the-int32-class-use-an-int – Chris Sinclair Aug 02 '13 at 14:02
6

Re: the recursive definition. There are certain types that are built into the Common Language Infrastructure (CLI). We're told:

The CLI built-in types have corresponding value types defined in the Base Class Library. They shall be referenced in signatures only using their special encodings (i.e., not using the general purpose valuetype TypeReference syntax). Partition I specifies the built-in types.

That is, the int will not be stored as a type reference to System.Int32 - it will be to the int provided by the CLI. But this is getting deep into the internals of the CLI/CLR. For any code that isn't part of the CLR/BCL, int and System.Int32 can be treated as synonymous.

If you want to pore over the .NET framework specs, they're downloadable from here (I was quoting from the MS Partition II.pdf document)

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • I wanted to mark this as answer. But Jason added a link later and I had no choice. But thank you for the links. That was a treasure trove. – msiyer Aug 03 '13 at 07:10
  • @msiyer - I hope the combination of answers are useful to people in the future. It's one of those questions that seems simple to answer until one actually starts trying to write an answer. – Damien_The_Unbeliever Aug 03 '13 at 07:56