快速问题:C# Linq“Single”陈述与“for”环形

发布于 2024-09-26 01:15:46 字数 1274 浏览 2 评论 0原文

我需要一些澄清。这两种方法是相同还是不同?我对何时更新按值传递到参数中的对象的引用以及何时创建新对象感到有点困惑。我知道该分配是否会创建新引用,但是更改属性怎么样?这两种方法都会以相同的方式更新字段“_someObjectList”吗?

 public class SomeObject{
     public Guid UniqueKey { get; set; }
     public object SomeProperty{ get; set; }
 }

 public class SomeObjectListWrapper{

    public SomeObjectListWrapper(List<SomeObject> someObjectList){
        _someObjectList = someObjectList;
    }

    private readonly List<SomeObject> _someObjectList;

    public void ReplaceItemPropertyValue1(Guid itemUniqueKey, object propertyValue)
    {

        List<int> resultIndices = new List<int>();
        for (var i = 0; i < _someObjectList.Count(); i++)
        {
            if (_someObjectList[i].UniqueKey == itemUniqueKey)
                resultIndices.Add(i);
        }

        if (resultIndices.Count != 1)
            throw new Exception(
                "just pretend this is the same exception as Single() throws when it can't find anything");
        _someObjectList[resultIndices[0]].SomeProperty = propertyValue;
    }

    public void ReplaceItemPropertyValue2(Guid itemUniqueKey, object propertyValue)
    {
        _someObjectList.Single(x=>x.UniqueKey==itemUniqueKey).SomeProperty=propertyValue;
    }
}

I need some clarification. Are these two methods the same or different? I get a little bit confused about when the reference to an object passed in a parameter by value is updated and when a new one is created. I know if that assignment creates a new reference, but what about changing a property? Will both of these methods update the field "_someObjectList" the same way?

 public class SomeObject{
     public Guid UniqueKey { get; set; }
     public object SomeProperty{ get; set; }
 }

 public class SomeObjectListWrapper{

    public SomeObjectListWrapper(List<SomeObject> someObjectList){
        _someObjectList = someObjectList;
    }

    private readonly List<SomeObject> _someObjectList;

    public void ReplaceItemPropertyValue1(Guid itemUniqueKey, object propertyValue)
    {

        List<int> resultIndices = new List<int>();
        for (var i = 0; i < _someObjectList.Count(); i++)
        {
            if (_someObjectList[i].UniqueKey == itemUniqueKey)
                resultIndices.Add(i);
        }

        if (resultIndices.Count != 1)
            throw new Exception(
                "just pretend this is the same exception as Single() throws when it can't find anything");
        _someObjectList[resultIndices[0]].SomeProperty = propertyValue;
    }

    public void ReplaceItemPropertyValue2(Guid itemUniqueKey, object propertyValue)
    {
        _someObjectList.Single(x=>x.UniqueKey==itemUniqueKey).SomeProperty=propertyValue;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独陪着我 2024-10-03 01:15:46

由于 SomeObject 是一个类(即引用类型),因此两个 ReplaceItemPropertyValue 方法都会更新插入到列表中的同一对象,并稍后从列表中检索该对象。 (如果 SomeObject 是结构/值类型,编译器将阻止您更新右值/返回值 [1]。)

作为一个小旁注,您的两个方法实际上并不相同。如果序列中存在多个匹配项,则 Single 方法会引发异常。要正确匹配行为,请改用 First


  1. “rvalue”实际上并不是“return value”的缩写,只是碰巧在这种情况下你的右值是一个返回值,这就是我指定这两个选项的原因。

Because SomeObject is a class (ie. a reference type), both ReplaceItemPropertyValue methods are updating the same object as was inserted into the list and will be retrieved from the list later. (If SomeObject was a struct/value type, the compiler would prevent you from updating an rvalue/return value [1].)

As a minor side-note, your two methods are not actually identical. The Single method raises an exception if there is more than one matching item in the sequence. To properly match the behaviour, use First instead.


  1. "rvalue" is not actually short for "return value," it just happens that in this case your rvalue is a return value, which is why I specified both options.
゛时过境迁 2024-10-03 01:15:46

根据您列表中的数据,他们可能会执行相同的操作。
ReplaceItemPropertyValue2 使用 Single 方法,如果未找到或多次找到 itemUnqiueKey,该方法将引发异常。

但只要 itemUniqueKey 在列表中不能出现多次,这两个函数就应该完成相同的任务。

They may do the same thing depending on the data in your list.
ReplaceItemPropertyValue2 uses the Single method which will throw an exception if itemUnqiueKey is not found or found more than once.

But as long as itemUniqueKey can't appear more than once in the list, the two functions should accomplish the same task.

戏蝶舞 2024-10-03 01:15:46

两者可能相同。

for 循环中的算法在键匹配时设置对象,然后中断。

而LINQ语句会将对象设置为所有键匹配的条目。这取决于您的收藏是否多次输入相同的密钥。

Both may be same.

The algorithm in the for loop set the object when key matches and then breaks out.

While the LINQ statement will set the object to all entries whose key match. It depends if your collection has same key entered more than once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文