快速问题:C# Linq“Single”陈述与“for”环形
我需要一些澄清。这两种方法是相同还是不同?我对何时更新按值传递到参数中的对象的引用以及何时创建新对象感到有点困惑。我知道该分配是否会创建新引用,但是更改属性怎么样?这两种方法都会以相同的方式更新字段“_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
SomeObject
是一个类(即引用类型),因此两个ReplaceItemPropertyValue
方法都会更新插入到列表中的同一对象,并稍后从列表中检索该对象。 (如果SomeObject
是结构/值类型,编译器将阻止您更新右值/返回值 [1]。)作为一个小旁注,您的两个方法实际上并不相同。如果序列中存在多个匹配项,则
Single
方法会引发异常。要正确匹配行为,请改用First
。Because
SomeObject
is a class (ie. a reference type), bothReplaceItemPropertyValue
methods are updating the same object as was inserted into the list and will be retrieved from the list later. (IfSomeObject
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, useFirst
instead.根据您列表中的数据,他们可能会执行相同的操作。
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.
两者可能相同。
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.