My code is the following
int tmpCnt;
if (name == "Dude")
tmpCnt++;
Why is there an error Use of unassigned local variable tmpCnt
? I know I didn't explicitly initialize it but due to Default Value Table a value type is initialized with 0
anyways. The reference also reminds me:
Remember that using uninitialized variables in C# is not allowed.
But why do I have to do it explicitly if it's already done by default? Wouldn't it gain performance if I wouldn't have to do it? Just wondering...
Local variables aren't initialized. You have to manually initialize them.
Members are initialized, for example:
But local variables are not:
So your code must be:
So the long and the short of it is, members are initialized, locals are not. That is why you get the compiler error.
The default value table only applies to initializing a variable.
Per the linked page, the following two methods of initialization are equivalent...
In your code, you merely defined the variable, but never initialized the object.