试图理解这一点:bool 变量是 C# 中的值类型
trying to understand this: bool variables are value types in C#
在C#中,bool、long等基本数据类型都是值类型。这意味着如果您声明一个 bool 变量并将另一个 bool 变量的值赋给它,您将在内存中有两个单独的 bool 值。
之后,如果您更改原始 bool 变量的值,则第二个 bool 变量的值不会更改。这些类型按值复制。 (来自专业 C# 5.0 Nagel、Christan、Glynn、Jay、Skinner、Morgan 的令牌)
所以如果我们有一个 class 如下:
public class Tutorial()
{
public bool param1;
}
然后在主要 class 我们有以下内容:
Tutorial x, y;
x = new Tutorial();
x.param1 = false;
y=x;
Console.WriteLine(y.param1);
y.param1 = true;
Console.WriteLine (x.param1);
结果显示 false
然后 true
我的问题是它不应该打印 false
false
吗?正如我在开头粘贴的文字中提到的那样?
my question is shouldnt it print false false?? as mentioned in the
text i paste at the beganing?
不是,因为x
和y
都是引用类型,指向同一个对象。这是由于此分配 y=x;
而发生的。因此,使用 x
更改 param1
的值等同于使用 y
.
"This means that if you declare a bool variable and assign it the
value of another bool variable, you will have two separate bool values
in memory"
bool x = true;
// We copy the value stored in x to the y
bool y = x;
// We change the x and the y doesn't change !
bool x = false;
// We verify the above with writing to the console
// the values of x and y
Console.WriteLine(x); // writes false to the console
Console.WriteLine(y); // writes true to the console.
如果你看
Tutorial x, y;
y=x;
您的 class 教程是参考类型。作业
y=x;
创建一个别名,其中 y 指的是与 x 相同的 Tutorial 实例。 bool 是值还是引用类型无关紧要,因为您正在修改同一 Tutorial 实例的 param1。
不,因为这一行:
y=x;
这意味着 y 和 x 都指向同一个对象(Tutorial
的一个实例),因此当您通过 x 引用该实例时,您通过 y 对该实例所做的任何更改也会反映出来。
在这种情况下,x 和 y 都是引用,因为 Tutorial
作为 class 是引用类型。
这表明是假的,假的:
public class Tutorial()
{
public bool param1;
public bool param2;
}
Tutorial x;
x = new Tutorial();
x.param1 = false;
x.param2=x.param1;
Console.WriteLine(x.param1);
x.param1 = true;
Console.WriteLine (x.param2);
在C#中,bool、long等基本数据类型都是值类型。这意味着如果您声明一个 bool 变量并将另一个 bool 变量的值赋给它,您将在内存中有两个单独的 bool 值。 之后,如果您更改原始 bool 变量的值,则第二个 bool 变量的值不会更改。这些类型按值复制。 (来自专业 C# 5.0 Nagel、Christan、Glynn、Jay、Skinner、Morgan 的令牌)
所以如果我们有一个 class 如下:
public class Tutorial()
{
public bool param1;
}
然后在主要 class 我们有以下内容:
Tutorial x, y;
x = new Tutorial();
x.param1 = false;
y=x;
Console.WriteLine(y.param1);
y.param1 = true;
Console.WriteLine (x.param1);
结果显示 false
然后 true
我的问题是它不应该打印 false
false
吗?正如我在开头粘贴的文字中提到的那样?
my question is shouldnt it print false false?? as mentioned in the text i paste at the beganing?
不是,因为x
和y
都是引用类型,指向同一个对象。这是由于此分配 y=x;
而发生的。因此,使用 x
更改 param1
的值等同于使用 y
.
"This means that if you declare a bool variable and assign it the value of another bool variable, you will have two separate bool values in memory"
bool x = true;
// We copy the value stored in x to the y
bool y = x;
// We change the x and the y doesn't change !
bool x = false;
// We verify the above with writing to the console
// the values of x and y
Console.WriteLine(x); // writes false to the console
Console.WriteLine(y); // writes true to the console.
如果你看
Tutorial x, y;
y=x;
您的 class 教程是参考类型。作业
y=x;
创建一个别名,其中 y 指的是与 x 相同的 Tutorial 实例。 bool 是值还是引用类型无关紧要,因为您正在修改同一 Tutorial 实例的 param1。
不,因为这一行:
y=x;
这意味着 y 和 x 都指向同一个对象(Tutorial
的一个实例),因此当您通过 x 引用该实例时,您通过 y 对该实例所做的任何更改也会反映出来。
在这种情况下,x 和 y 都是引用,因为 Tutorial
作为 class 是引用类型。
这表明是假的,假的:
public class Tutorial()
{
public bool param1;
public bool param2;
}
Tutorial x;
x = new Tutorial();
x.param1 = false;
x.param2=x.param1;
Console.WriteLine(x.param1);
x.param1 = true;
Console.WriteLine (x.param2);