gpt4 book ai didi

c# - 如何在 C# 中使用 linq/lambda 获取数据副本而不是引用?

转载 作者:太空狗 更新时间:2023-10-29 20:08:20 25 4
gpt4 key购买 nike

是否有一种简单的方法基本上只获取数据的副本而不是使用此方法的引用?我尝试了 .ToArray().Where() 但它似乎仍然传递了一个引用。

例子:

static void Main(string[] args)
{
List<ob> t = new List<ob>();
t.Add(new ob() { name = "hello" });
t.Add(new ob() { name = "test" });

ob item = t.Where(c => c.name == "hello").First();

// Changing the name of the item changes the original item in the list<>
item.name = "burp";

foreach (ob i in t)
{
Console.WriteLine(i.name);
}

Console.ReadLine();
}

public class ob
{
public string name;
}

最佳答案

您需要自己创建 ob 的副本 - LINQ 不提供此功能。

关于c# - 如何在 C# 中使用 linq/lambda 获取数据副本而不是引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8724421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com