Sunday, October 19, 2014

Not So Primitive Types

A few nuances about the primitive types in .Net which are not so obvious:
  • If you add two shorts you will get int. The same with bytes. Arithmetic operators for these types are not implemented in .Net therefore the values are implicitly converted to int before calculation.
        short x = 2, y = 2;
        var z = x + y;
        Console.WriteLine(z.GetType()); //output: System.Int32 
  • Chuck Norris is not the only person who can divide by zero. Every .Net developer can do this. Just use floating point numbers.
        var z = 1.0 / 0;
        Console.WriteLine(z); //output: Infinity