两个数组的元素引用一个对象
Two array's element reference to an object
我有以下代码:
struct NewType
{
public int val;
}
static void Main(string[] args)
{
NewType i = new NewType();
List<NewType> IList = new List<NewType>();
i.val = 1;
IList.Add(i);
i.val = 2;
IList.Add(i);
}
之后,如果我打印 IList 列表中的每个元素,结果将是 12
和我想的相反22
因为:
有人告诉我为什么结果是12?
基本数据类型与抽象数据类型的工作方式不同。 int 不是引用,因此与非基本数据类型不同,它们实际上是被复制的。
类型NewType
是值类型,不是引用类型。这意味着类型为 List<NewType>
的 IList
保存值的副本,而不是对它们的引用。话虽这么说,你的照片不正确。
After that, If I print each of element in IList list, the result will
be 12 It's opposite than what I thought 22
这是预期的。
这里
i.val = 1;
IList.Add(i);
您在 IList
中添加了 i 值的副本。此副本的 val 值为 1.
然后
i.val = 2;
IList.Add(i);
您通过将值 2 复制到它来更改 val
的值。之后您将 i
的副本添加到 IList
。此副本的 val 值为 2.
为了让您注意到您在问题中的描述,类型 NewType
应该是引用类型。如果将 NewType
的定义更改为以下定义:
class NewType
{
public int val;
}
你会注意到你所描述的。
因为 NewType
是一个 struct
而 struct 是一个值类型而不是像 class 这样的引用类型。如果你有 class
而不是 struct 你会得到 22
,这个 post 会帮助你理解。
这是因为NewType
是一个struct
,作为值类型添加到列表中(添加到列表中的是对象的副本,而不是对原始对象的引用).
如果您将它从 struct
更改为 class
那么它就会如您所料。 class 通过引用传递。
看看Classes and Structs (C# Programming Guide)
A struct is a value type. When a struct is created, the variable to
which the struct is assigned holds the struct's actual data. When the
struct is assigned to a new variable, it is copied. The new variable
and the original variable therefore contain two separate copies of the
same data. Changes made to one copy do not affect the other copy.
IList.Add(i)
隐式执行 i
的浅表复制,因为 NewType
是值类型(结构)。在执行浅拷贝时,它还会按值复制字段 NewType.val
。所以 IList
包含两个不同的 NewType
值,它们本身包含一个不同的 val
整数。
如果您将 NewType
从 struct
更改为 class
,那么您将得到您所期望的结果。
我有以下代码:
struct NewType
{
public int val;
}
static void Main(string[] args)
{
NewType i = new NewType();
List<NewType> IList = new List<NewType>();
i.val = 1;
IList.Add(i);
i.val = 2;
IList.Add(i);
}
之后,如果我打印 IList 列表中的每个元素,结果将是 12 和我想的相反22
因为:
有人告诉我为什么结果是12?
基本数据类型与抽象数据类型的工作方式不同。 int 不是引用,因此与非基本数据类型不同,它们实际上是被复制的。
类型NewType
是值类型,不是引用类型。这意味着类型为 List<NewType>
的 IList
保存值的副本,而不是对它们的引用。话虽这么说,你的照片不正确。
After that, If I print each of element in IList list, the result will be 12 It's opposite than what I thought 22
这是预期的。
这里
i.val = 1;
IList.Add(i);
您在 IList
中添加了 i 值的副本。此副本的 val 值为 1.
然后
i.val = 2;
IList.Add(i);
您通过将值 2 复制到它来更改 val
的值。之后您将 i
的副本添加到 IList
。此副本的 val 值为 2.
为了让您注意到您在问题中的描述,类型 NewType
应该是引用类型。如果将 NewType
的定义更改为以下定义:
class NewType
{
public int val;
}
你会注意到你所描述的。
因为 NewType
是一个 struct
而 struct 是一个值类型而不是像 class 这样的引用类型。如果你有 class
而不是 struct 你会得到 22
,这个 post 会帮助你理解。
这是因为NewType
是一个struct
,作为值类型添加到列表中(添加到列表中的是对象的副本,而不是对原始对象的引用).
如果您将它从 struct
更改为 class
那么它就会如您所料。 class 通过引用传递。
看看Classes and Structs (C# Programming Guide)
A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.
IList.Add(i)
隐式执行 i
的浅表复制,因为 NewType
是值类型(结构)。在执行浅拷贝时,它还会按值复制字段 NewType.val
。所以 IList
包含两个不同的 NewType
值,它们本身包含一个不同的 val
整数。
如果您将 NewType
从 struct
更改为 class
,那么您将得到您所期望的结果。