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:
为什么有错误使用未分配的局部变量tmpCnt?我知道我没有明确地初始化它,但是由于默认值表,值类型无论如何都被初始化为0。该参考文献也提醒我:
Remember that using uninitialized variables in C# is not allowed.
请记住,不允许在C#中使用未初始化的变量。
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 个解决方案
#1
97
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.
所以它的长期和短期是,成员被初始化,当地人不是。这就是你得到编译器错误的原因。
#2
9
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.
默认分配适用于类成员,但不适用于局部变量。正如Eric Lippert在这个答案中解释的那样,微软可能默认初始化了本地人,但他们选择不这样做,因为使用未分配的本地几乎肯定是一个错误。
#3
5
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.
- 输出参数,包括struct实例构造函数的this变量。
- Local variables , except those declared in a catch clause or a foreach statement.
- 局部变量,但在catch子句或foreach语句中声明的变量除外。
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.
- 在catch子句或foreach语句中声明的变量。
#4
1
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.
虽然值类型具有默认值且不能为null,但还需要显式初始化它们才能使用。您可以将这两个规则视为并排规则。值类型不能为null ==>编译器保证这一点。如果你问怎么样?你得到的错误信息就是答案。一旦调用了它们的构造函数,就会使用它们的默认值进行初始化。
int tmpCnt; // not accepted
int tmpCnt = new Int(); // defualt value applied tmpCnt = 0
#5
0
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.
如果要初始化局部变量,则需要显式初始化局部变量。在这种情况下,(如链接文档所述)通过设置值0或使用new运算符。
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.
您显示的代码确实尝试在将变量初始化为任何内容之前使用变量tmpCnt的值,并且编译器正确地警告它。
#6
0
The default value table only applies to initializing a variable.
默认值表仅适用于初始化变量。
Per the linked page, the following two methods of initialization are equivalent...
根据链接页面,以下两种初始化方法是等效的......
int x = 0;
int x = new int();
In your code, you merely defined the variable, but never initialized the object.
在您的代码中,您只是定义了变量,但从未初始化该对象。
#7
0
See this thread concerning uninitialized bools, but it should answer your question.
有关未初始化的bool,请参阅此主题,但它应该回答您的问题。
Local variables are not initialized unless you call their constructors (new) or assign them a value.
除非您调用其构造函数(new)或为其赋值,否则不会初始化局部变量。
#8
0
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.
在使用它们之前必须明确分配它们。它减少了使用你认为已经给出合理值的变量的可能性,实际上它有一些默认值。
#1
97
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.
所以它的长期和短期是,成员被初始化,当地人不是。这就是你得到编译器错误的原因。
#2
9
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.
默认分配适用于类成员,但不适用于局部变量。正如Eric Lippert在这个答案中解释的那样,微软可能默认初始化了本地人,但他们选择不这样做,因为使用未分配的本地几乎肯定是一个错误。
#3
5
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.
- 输出参数,包括struct实例构造函数的this变量。
- Local variables , except those declared in a catch clause or a foreach statement.
- 局部变量,但在catch子句或foreach语句中声明的变量除外。
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.
- 在catch子句或foreach语句中声明的变量。
#4
1
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.
虽然值类型具有默认值且不能为null,但还需要显式初始化它们才能使用。您可以将这两个规则视为并排规则。值类型不能为null ==>编译器保证这一点。如果你问怎么样?你得到的错误信息就是答案。一旦调用了它们的构造函数,就会使用它们的默认值进行初始化。
int tmpCnt; // not accepted
int tmpCnt = new Int(); // defualt value applied tmpCnt = 0
#5
0
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.
如果要初始化局部变量,则需要显式初始化局部变量。在这种情况下,(如链接文档所述)通过设置值0或使用new运算符。
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.
您显示的代码确实尝试在将变量初始化为任何内容之前使用变量tmpCnt的值,并且编译器正确地警告它。
#6
0
The default value table only applies to initializing a variable.
默认值表仅适用于初始化变量。
Per the linked page, the following two methods of initialization are equivalent...
根据链接页面,以下两种初始化方法是等效的......
int x = 0;
int x = new int();
In your code, you merely defined the variable, but never initialized the object.
在您的代码中,您只是定义了变量,但从未初始化该对象。
#7
0
See this thread concerning uninitialized bools, but it should answer your question.
有关未初始化的bool,请参阅此主题,但它应该回答您的问题。
Local variables are not initialized unless you call their constructors (new) or assign them a value.
除非您调用其构造函数(new)或为其赋值,否则不会初始化局部变量。
#8
0
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.
在使用它们之前必须明确分配它们。它减少了使用你认为已经给出合理值的变量的可能性,实际上它有一些默认值。