[英]List.Add(x). Is x a REFERENCE or a VALUE?
Regarding this data and (pseudo) code: 关于此数据和(伪)代码:
List<myClass> Periods = new List<myClass>();
// contents of Periods is:
Periods[0] = "000";
Periods[1] = "111";
Periods[2] = "222";
Periods[3] = "REST"; // REST PERIOD
Periods[4] = "444";
Periods[5] = "REST"; // REST PERIOD
Periods[6] = "666";
Periods[7] = "777";
Periods[8] = "888";
The following code iterates through the list, and pulls out multiple list elements as they occur between REST periods into individual lists, and adds these lists to a "list of lists". 以下代码遍历列表,并在REST周期之间将多个列表元素拉出到单个列表中,并将这些列表添加到“列表列表”中。 There may be one or more List Entries between rest periods.
休息时段之间可能有一个或多个列表条目。
List<List<Period>> DutyDayPeriods = new List<List<Period>>();
List<Period> thisList = new List<Period>();
for (int i = 0; i < Periods.Count; i++ ) {
thisList.Add(Periods[i]);
if (Periods[i].Rest == "REST") {
DutyDayPeriods.Add(thisList);
thisList.Clear();
}
}
At the bottom of the for loop, DutyDayPeriods<> contains 1-n identical copies of "thisList". 在for循环的底部,DutyDayPeriods <>包含1-n个相同的“thisList”副本。 Essentially the last set of list items between the last rest period and the end of the list:
基本上是最后一个休息期和列表结尾之间的最后一组列表项:
So, I am expecting the following: 所以,我期待以下内容:
DutyDayPeriods[0]
Period 000
Period 111
Period 222
DutyDayPeriods[1]
Period 444
DutyDayPeriods[2]
Period 666
Period 777
Period 888
But I'm actually getting: 但我实际上得到了:
DutyDayPeriods[0]
Period 666
Period 777
Period 888
DutyDayPeriods[1]
Period 666
Period 777
Period 888
DutyDayPeriods[2]
Period 666
Period 777
Period 888
thisList
is behaving like a reference , not a value . thisList
的行为类似于引用 ,而不是值 。
Every time thisList
changes, it seems to change retroactively to items that have already been added to DutyDayPeriods
. 每次
thisList
更改时,它似乎会追溯更改为已添加到DutyDayPeriods
。
Is this the case? 是这样的吗? And if so, how do I accomplish what I am trying to do?
如果是这样,我该如何完成我想做的事情?
Thanks. 谢谢。
EDIT: 编辑:
I modified the following to prevent the rest periods themselves from being included. 我修改了以下内容以防止包含休息期。 Just in case one of you notices that.
以防你们其中一个人注意到这一点。 ;-)
;-)
if (Periods[i].Rest != "REST") {
thisList.Add(Periods[i]);
}
else {
DutyDayFlights.Add(thisList);
thisList = new List<Period>();
}
Yes, a List<T>
is always a reference. 是的,
List<T>
始终是引用。
You just need to replace .Clear()
with creating a new list. 您只需要使用创建新列表替换
.Clear()
。
DutyDayPeriods.Add(thisList);
//thisList.Clear();
thisList = new List<Period>();
The short answer is yes, you're working with references. 简短的回答是肯定的,你正在使用参考文献。 The longer answer is more complicated.
答案越长越复杂。 C# handles simple types (
int
and char
for example) and structs by value but class objects are always handled by reference. C#按值处理简单类型(例如
int
和char
)和结构,但类对象总是通过引用处理。 So, when you call thisList.Add
with a Period
object, you're actually sending a reference to that object. 因此,当您使用
Period
对象调用thisList.Add
时,您实际上是在发送对该对象的引用。 If what you actually want is the value, you should do something like this: 如果你真正想要的是价值,你应该这样做:
DutyDayPeriods.Add(thisList);
thisList = new List<Period>();
This will dereference everything on thisList
from thisList
, meaning that the only remaining "active" references will be on your other collection. 这将间接引用的一切
thisList
从thisList
,这意味着剩下的唯一“积极”的引用将在您的其他集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.