The answer to "why does C# work this way" is invariably "because that's what's in the spec." Section 5.3.2 of the C# language specification lists the variables that are not initially assigned:
- Instance variables of initially unassigned struct variables.
- Output parameters, including the this variable of struct instance constructors.
- Local variables, except those declared in a catch clause or a foreach statement.
As to why this is an error, Section 5.3 states
A variable must be definitely assigned at each location where its value is obtained.
If you initialize the value as int res = new int();
, you will get the default value of zero. The more common way is mentioned in other answers, which is to set it to zero explicitly. Relying on default values just makes code less readable with no real advantages.