如果我有一个列表对象并且得到了引用?用我的查找功能从列表中删除:
private XTD.XTD_DATA_DESCRIPTION FindDataDescription(string szName)
{
string szFullName;
foreach (XTD.XTD_DATA_DESCRIPTION dd in m_DataDescription)
{
szFullName = dd.Root + "\\" + dd.Name;
if(szFullName.Length > 1024)
{
szFullName.Substring(0, 1024);
}
if(szFullName.CompareTo(szName)==0)
{
return dd;
}
}
return new XTD.XTD_DATA_DESCRIPTION();
} // FindDataDescription
并像这样使用它:
pDataDescription = FindDataDescription(szName);
if (pDataDescription.Name == null)
{
bError = true;
break;
}
switch (pDataDescription.TransmissionRateFormat)
{
case XTD.eXTDTransmissionRateFormat.eXTDTransmissionRateFormatTimeInterval:
{
uLength = 4;
hbyte = new byte[uLength];
Array.Copy(received, uIndex, hbyte, 0, hbyte.Length);
pDataDescription.TRF5CurrentValue.uint32 = BitConverter.ToUInt32(hbyte, 0);
if (pDataDescription.TRF5CurrentValue.uint32 == 0)
{
pDataDescription.TRF5CurrentValue.uint32 = pDataDescription.TRF5DefaultTransmissionRate.uint32;
}
uIndex += uLength;
break;
}
//...
它在我的收藏中仍然有旧值,但是pDataDescription得到了新值。 我在Google上搜索了一下(它以及一些我精通C#的朋友)说它应该起作用,但是没有用。那么这是参考吗?还是我只是错了?
我调试了,但find函数什么也没找到!
答案 0 :(得分:0)
是的,您会获得参考,请使用以下代码:
void Main()
{
List<Foo> foos = new List<Foo>
{
new Foo { Bar = 8 },
new Foo { Bar = 9 },
new Foo { Bar = 10 }
};
"Foos before we find and modify".Print();
foos.ForEach(f => f.Bar.Print());
var foo = FindObject(foos, 8);
if(foo != null)
foo.Bar = -1;
"Foos after we find and modify".Print();
foos.ForEach(f => f.Bar.Print());
}
public Foo FindObject(List<Foo> foos, int bar) => foos.FirstOrDefault(f => f.Bar == bar);
public class Foo
{
public int Bar { get; set;}
}
这将输出:
Foos before we find and modify
8
9
10
Foos after we find and modify
-1
9
10