C# 局部变量
C# Local variables
首先是代码:
if (A1.Text != "" || A1.Text != null)
{
string D1 = "<"+A1.Text+">";
}
else
{
string D1 = "null";
}
现在我正在尝试稍后引用 D1。
例如this.label1.Text = D1;
引用和定义字符串的 if 语句在同一个父块中,为什么我不能使用 D1?
您需要将声明移动到父块中,如下所示:
string D1;
if (A1.Text != "" || A1.Text != null)
{
D1 = "<"+A1.Text+">";
}
else
{
D1 = "null";
}
// Then you can reference `D1` later in the same method.
this.label1.Text = D1;
A 变量只能在定义范围或子范围内访问。以这种方式重构您的代码 -
string D1;
if (A1.Text != "" || A1.Text != null)
{
D1 = "<"+A1.Text+">";
}
else
{
D1 = "null";
}
添加到上面的答案
请查看此link以获得更多启示
Compilers are in the business of generating code which manages the storage of the data manipulated by that program. There are lots of different ways of generating code to manage memory, but over time two basic techniques have become entrenched.
The first is to have some sort of "long lived" storage area where the "lifetime" of each byte in the storage -- that is, the period of time when it is validly associated with some program variable -- cannot be easily predicted ahead of time. The compiler generates calls into a "heap manager" that knows how to dynamically allocate storage when it is needed and reclaim it when it is no longer needed.
The second is to have some sort of "short lived" storage area where the lifetime of each byte in the storage is well known, and, in particular, lifetimes of storages follow a "nesting" pattern. That is, the allocation of the longest-lived of the short-lived variables strictly overlaps the allocations of shorter-lived variables that come after it.
Local variables follow the latter pattern; when a method is entered, its local variables come alive. When that method calls another method, the new method's local variables come alive. They'll be dead before the first method's local variables are dead. The relative order of the beginnings and endings of lifetimes of storages associated with local variables can be worked out ahead of time.
出于这个原因,局部变量通常作为存储在 "stack" 数据结构上生成,因为堆栈具有 属性 ,即第一个压入它的东西将是最后一个东西弹出。
所以整体的局部变量都是短暂的并且通常存储在堆栈中,为什么堆栈比其他的更有效率。 Link 了解更多信息,了解为什么堆叠。所以你应该按照之前的建议扩大它的范围
string D1;
if (A1.Text != "" || A1.Text != null)
{
D1 = "<"+A1.Text+">";
}
为了简化代码你也可以这样写
if (!string.IsNullOrWhiteSpace(A1.Text))
{
label1.Text = "<" + A1.Text + ">";
}
else
{
label1.Text = "null";
}
首先是代码:
if (A1.Text != "" || A1.Text != null)
{
string D1 = "<"+A1.Text+">";
}
else
{
string D1 = "null";
}
现在我正在尝试稍后引用 D1。
例如this.label1.Text = D1;
引用和定义字符串的 if 语句在同一个父块中,为什么我不能使用 D1?
您需要将声明移动到父块中,如下所示:
string D1;
if (A1.Text != "" || A1.Text != null)
{
D1 = "<"+A1.Text+">";
}
else
{
D1 = "null";
}
// Then you can reference `D1` later in the same method.
this.label1.Text = D1;
A 变量只能在定义范围或子范围内访问。以这种方式重构您的代码 -
string D1;
if (A1.Text != "" || A1.Text != null)
{
D1 = "<"+A1.Text+">";
}
else
{
D1 = "null";
}
添加到上面的答案
请查看此link以获得更多启示
Compilers are in the business of generating code which manages the storage of the data manipulated by that program. There are lots of different ways of generating code to manage memory, but over time two basic techniques have become entrenched.
The first is to have some sort of "long lived" storage area where the "lifetime" of each byte in the storage -- that is, the period of time when it is validly associated with some program variable -- cannot be easily predicted ahead of time. The compiler generates calls into a "heap manager" that knows how to dynamically allocate storage when it is needed and reclaim it when it is no longer needed.
The second is to have some sort of "short lived" storage area where the lifetime of each byte in the storage is well known, and, in particular, lifetimes of storages follow a "nesting" pattern. That is, the allocation of the longest-lived of the short-lived variables strictly overlaps the allocations of shorter-lived variables that come after it.
Local variables follow the latter pattern; when a method is entered, its local variables come alive. When that method calls another method, the new method's local variables come alive. They'll be dead before the first method's local variables are dead. The relative order of the beginnings and endings of lifetimes of storages associated with local variables can be worked out ahead of time.
出于这个原因,局部变量通常作为存储在 "stack" 数据结构上生成,因为堆栈具有 属性 ,即第一个压入它的东西将是最后一个东西弹出。
所以整体的局部变量都是短暂的并且通常存储在堆栈中,为什么堆栈比其他的更有效率。 Link 了解更多信息,了解为什么堆叠。所以你应该按照之前的建议扩大它的范围
string D1;
if (A1.Text != "" || A1.Text != null)
{
D1 = "<"+A1.Text+">";
}
为了简化代码你也可以这样写
if (!string.IsNullOrWhiteSpace(A1.Text))
{
label1.Text = "<" + A1.Text + ">";
}
else
{
label1.Text = "null";
}