Why did I get the compile error "Use of unassigned local variable"?
Solution 1:
Local variables aren't initialized. You have to manually initialize them.
Members are initialized, for example:
public class X
{
private int _tmpCnt; // This WILL initialize to zero
...
}
But local variables are not:
public static void SomeMethod()
{
int tmpCnt; // This is not initialized and must be assigned before used.
...
}
So your code must be:
int tmpCnt = 0;
if (name == "Dude")
tmpCnt++;
So the long and the short of it is, members are initialized, locals are not. That is why you get the compiler error.
Solution 2:
Default assignments apply to class members, but not to local variables. As Eric Lippert explained it in this answer, Microsoft could have initialized locals by default, but they choose not to do it because using an unassigned local is almost certainly a bug.