对象属于调用外部对象类的列表
我想简化以下代码行
int TotalObjectVarN3 = (Object1.variable3 + Object2.variable3 + Object3.variable3 + Object3.variable3);
最佳答案
This overload的System.Linq.Enumerable.Sum
应该很适合你:
Computes the sum of the sequence of Int32 values that are obtained by invoking a transform function on each element of the input sequence.
在这种情况下,“转换函数”将是 Func<TSource, Int32>
返回 variable3
的值项目的属性(例如 obj => obj.variable3
)。
你可以这样使用它:
int TotalObjectVarN3 = new []{ Object1, Object2, Object3 }.Sum(obj => obj.variable3);
关于c# - 从列表中添加对象变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69953930/