So, Iv’e been learning C# lately but there is one thing I cant figure out or find an answer to:
Consider this:
class Class1 {
int myInt = 32;
}
and this:
class Class1 {
int myInt;
public Class1(){
myInt = 32;
}
}
I would simply like to know when and why I should use one method over the other for assigning or instantiating values.
It is a matter of taste or coding standards of your company.
My rule of thumb is that if all my constructors assign the same value to a variable, I use the first form; if the value comes from outside, or different constructors assign different values to a variable, I use the second form.