Search code examples
c#value-typereference-type

What is int in c#?


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;

Solution

  • 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?