1

What is int in C#? Is it a keyword, or is it a class derived from system.ValueTypes? If it is a keyword then how do the following lines compile

int i = new int(); // If int is not a class then how does it have a default constructor

Console.WriteLine(i.ToString()); // If int is not a class then how does it have member functions

If int is a class then why is it not necessary to always initialize it with new? How does the following line compile?

int i = 8;
user1232138
  • 5,451
  • 8
  • 37
  • 64
  • 3
    Who says "structs" or "value-types" *can't* have Methods? That's some Java nonsense talking :) `int` is `System.Int32` which is a `struct`-type. (To make life simpler the compiler knows about various "literal" constructs like integers, floating point values, and strings. However, it's a fixed set. E.g. there is no "literal" for a GUID, so `new Guid(string)` is required.) –  Jul 21 '12 at 04:08
  • 1
    All value types can have methods, Java is the silly exception. – Ry- Jul 21 '12 at 04:11
  • `int i = 8` compiles for the same reason that `string foo = "bar"` compiles. `8` is a literal of the type `int`, just like `"bar"` is a literal of the type `string`. – cdhowie Jul 21 '12 at 04:12
  • Well if int is an alias for a _struct_ `System.Int32`, then how does it inherit from the `System.Object` class?? – user1232138 Jul 21 '12 at 04:14
  • @user1232138 Every `struct` is a sub-type of `ValueType` and `ValueType` is a sub-type of `object`. Unlike a `class` other sub-types cannot be specified with a `struct` (although interfaces can be specified). This is how the Type tree is defined in C#/CLR. See http://stackoverflow.com/questions/11461789/base-class-of-struct-construct-in-c-sharp Of course, this isn't the entire story. `object x = 1` "boxes" (keyword) the `int` value, for instance. –  Jul 21 '12 at 04:19
  • But in C#, structures cannot inherit from other structures or classes then how does `int` inherit from `System.Object`.. – user1232138 Jul 21 '12 at 04:22
  • @user1232138 They can. And they do. But they have a *fixed* hierarchy. That is, there is a default: `struct -> (abstract class) System.ValueType -> (class) System.Object` which *cannot* be changed, just as one cannot make a class *not* ultimately derived from `object`. Every `struct` value *is a* `ValueType`. Every `ValueType` *is a* `object`. Thus every `struct` value *is a* `object`. –  Jul 21 '12 at 04:22
  • (Oops, I meant: "`ValueType`, an abstract class, *is a* sub-type of `object`.") –  Jul 21 '12 at 04:28

3 Answers3

5

The int is an alias of CTS type System.Int32 structure.

Read SO answer posted by Eric Lippert - How do ValueTypes derive from Object (ReferenceType) and still be ValueTypes?

Community
  • 1
  • 1
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • Well if int is an alias for a _struct_ `System.Int32`, then how does it inherit from the `System.Object` class?? – user1232138 Jul 21 '12 at 04:15
3

int is a keyword that is an alias for the value type (struct) System.Int32.

Being a regular value type, I believe its inheritance is System.Object -> System.ValueType -> System.Int32.

Because int has a literal notation, like strings and other number types, you can create instances of it without new.

Gabe
  • 84,912
  • 12
  • 139
  • 238
0

To explain code with code:

Console.WriteLine(typeof(int) == typeof(Int32)); // Outputs: True
Console.WriteLine(typeof(int).Name); // Outputs: Int32
Luis Perez
  • 27,650
  • 10
  • 79
  • 80