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 anyway. 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?
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.