Well, in C # you have to differentiate between "Fields" and "Local Variables". The fields do not need to be initialized, while the local variables do. We see it in an example:
class Prueba
{
int i; //Al estar definido en la clase, es un Campo
public Prueba()
{
i+=1;
}
}
This example compiles perfectly, and i
is initialized with the default value of int
, or 0
However, the following example does not compile:
class Prueba
{
public Prueba()
{
int i; //Al estar definido en un método, es una variable local
i+=1;
}
}
This is done to ensure the reliability of the application, and is perfectly explained in the C # language reference in the 5.3 Definitive Assignment
Regarding VB.Net, it is a language that has inherited certain habits of the original Visual Basic, among them the need not to initialize the local variables. It is a language in which it is somewhat easier to shoot yourself in the foot, and for that reason it is recommended to activate the options Option Strict
and Option Explicit
, to prevent problems difficult to debug.
Summing up: Why is it like that? Because when both languages were defined, it was decided that it should be like that, it is not necessary to give it many more laps. C # is a new language that has learned from errors of previous languages, while VB.net had to make some concessions to make the transition from Visual Basic easier.