C# - 获取列表<byte> 参考索引
C# - Getting List<byte> reference index
我有一个列表>
List<List<Byte>> bytes = new List<List<Byte>>()
{
new List<Byte> {1, 1, 2, 3, 4}, // index 0 is original
new List<Byte> {0, 0, 2, 4, 1},
new List<Byte> {1, 2, 2, 1, 1},
new List<Byte> {1, 0, 2, 2, 2}
};
而且第一个列表是原创的。然后我对我的列表进行排序,我必须找到我原来的列表索引。
bytes:
[0] = {0, 0, 2, 4, 1}
[1] = {1, 0, 2, 2, 2}
[2] = {1, 1, 2, 3, 4} // here is that index
[3] = {1, 2, 2, 1, 1}
有人建议我先对未排序的列表使用引用,然后对列表进行排序并使用 ReferenceEqual,但这对我不起作用。我是不是通过设置引用做错了什么,或者它在 List 上不起作用?我怎样才能得到排序数组中原始的索引? P.S 我正在使用 OrderBy 和 IComparer 进行排序。
这是我尝试参考的方式:
List<byte> reference = new List<byte>(bytes[0]);
This is how I try to make reference:
List<byte> reference = new List<byte>(bytes[0]);
这不是创建引用的正确方法,因为您通过调用 List<byte>
的构造函数来创建副本。 bytes[0]
的副本不存在于 bytes
中,因此您无法通过检查引用相等性来找到它。
您应该这样做:
List<byte> reference = bytes[0];
现在reference
在排序前引用了位置为零的列表,因此您应该能够使用引用相等性找到它的索引。
我有一个列表>
List<List<Byte>> bytes = new List<List<Byte>>()
{
new List<Byte> {1, 1, 2, 3, 4}, // index 0 is original
new List<Byte> {0, 0, 2, 4, 1},
new List<Byte> {1, 2, 2, 1, 1},
new List<Byte> {1, 0, 2, 2, 2}
};
而且第一个列表是原创的。然后我对我的列表进行排序,我必须找到我原来的列表索引。
bytes:
[0] = {0, 0, 2, 4, 1}
[1] = {1, 0, 2, 2, 2}
[2] = {1, 1, 2, 3, 4} // here is that index
[3] = {1, 2, 2, 1, 1}
有人建议我先对未排序的列表使用引用,然后对列表进行排序并使用 ReferenceEqual,但这对我不起作用。我是不是通过设置引用做错了什么,或者它在 List 上不起作用?我怎样才能得到排序数组中原始的索引? P.S 我正在使用 OrderBy 和 IComparer 进行排序。
这是我尝试参考的方式:
List<byte> reference = new List<byte>(bytes[0]);
This is how I try to make reference:
List<byte> reference = new List<byte>(bytes[0]);
这不是创建引用的正确方法,因为您通过调用 List<byte>
的构造函数来创建副本。 bytes[0]
的副本不存在于 bytes
中,因此您无法通过检查引用相等性来找到它。
您应该这样做:
List<byte> reference = bytes[0];
现在reference
在排序前引用了位置为零的列表,因此您应该能够使用引用相等性找到它的索引。