using System;
namespace FirstApplication
{
class Program
{
static void Main(params string[] args)
{
int x = new int();
x = 12;
//int y = new int(12);
Console.WriteLine(x);
}
}
}
By design, why isn’t there a single parameter ctor for int such that x can be set to 12 without having to be set to 0 first?
The reason lies in the CLR itself. The CLR does not treat
Int32as any ordinary struct. It has a special type for storingint, which is not a CLR object. This means that it does not need a contructor, just like anintin C does not have a constructor.Also, the CLR handling of struct default contructors is to set all fields to their default value. For int this is 0.
new int()does seem to be a special case, simply being an ‘alias’ for 0, eliminating any constructor altogether.Plus, this is just plain better.