Why compile error “Use of unassigned local variabl

2018-12-31 08:43发布

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

8条回答
人间绝色
2楼-- · 2018-12-31 09:00

Local variables are not automatically initialized. That only happens with instance-level variables.

You need to explicitly initialize local variables if you want them to be initialized. In this case, (as the linked documentation explains) either by setting the value of 0 or using the new operator.

The code you've shown does indeed attempt to use the value of the variable tmpCnt before it is initialized to anything, and the compiler rightly warns about it.

查看更多
人间绝色
3楼-- · 2018-12-31 09:01

See this thread concerning uninitialized bools, but it should answer your question.

Local variables are not initialized unless you call their constructors (new) or assign them a value.

查看更多
无与为乐者.
4楼-- · 2018-12-31 09:06

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.

查看更多
心情的温度
5楼-- · 2018-12-31 09:06

local variables don't have a default value.

They have to be definitely assigned before you use them. It reduces the chance of using a variable you think you've given a sensible value to, when actually it's got some default value.

查看更多
无色无味的生活
6楼-- · 2018-12-31 09:09

While value types have default values and can not be null, they also need to be explicitly initialized in order to be used. You can think of these two rules as side by side rules. Value types can NOT be null==> the compiler guarantes that. If you ask how? the error message you got is the answer. Once you call their constructors, they got inialized with their default values.

int tmpCnt; // not accepted 
int tmpCnt = new Int(); // defualt value applied tmpCnt = 0 
查看更多
明月照影归
7楼-- · 2018-12-31 09:11

The following categories of variables are classified as initially unassigned:

  • Instance variables of initially unassigned struct variables.
  • Output parameters, including the this variable of struct instance constructors.
  • Local variables , except those declared in a catch clause or a foreach statement.

The following categories of variables are classified as initially assigned:

  • Static variables.
  • Instance variables of class instances.
  • Instance variables of initially assigned struct variables.
  • Array elements.
  • Value parameters.
  • Reference parameters.
  • Variables declared in a catch clause or a foreach statement.
查看更多
登录 后发表回答