简体   繁体   中英

Is there a more elegant way to sum multiple properties?

I have a class which contains multiple properties of type Int32:

public class MyClass
{
    public int C1 { get; set; }
    public int C2 { get; set; }
    public int C3 { get; set; }
    .
    .
    .
    public int Cn { get; set; }
}

I want to sum all this properties. Instead of doing:

int sum = C1 + C2 + C3 + ... + Cn

is there a more efficient/elegant method?

You can fake it, but I'm not sure how useful it is:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            var test = new MyClass();
            // ...
            int sum = test.All().Sum();
        }
    }

    public class MyClass
    {
        public int C1 { get; set; }
        public int C2 { get; set; }
        public int C3 { get; set; }
        // ...
        public int Cn { get; set; }

        public IEnumerable<int> All()
        {
            yield return C1; 
            yield return C2; 
            yield return C3; 
            // ...
            yield return Cn; 
        }
    }
}                                                                                            

If you really want to perform the sum without having to type each property you can use reflection to iterate through your properties but this is involves a big performance cost. However, for fun you can do something like this:

var item = new MyClass();
// Populate the values somehow
var result = item.GetType().GetProperties()
    .Where(pi => pi.PropertyType == typeof(Int32))
    .Select(pi => Convert.ToInt32(pi.GetValue(item, null)))
    .Sum();

PS: Don't forget to add using System.Reflection; directive.

Maybe you can use an array or a data structure which has the IEnumarable interfaces vs a custom class. Then you can use linq to do Sum().

If there's a strong enough need to store the values in separate members (properties, fields), then yes, that's the only way. If you have a list of numbers however, store them in a list, not in separate members.

Or, ugly:

new[]{C1,C2,C3,C4}.Sum()

But more characters than the single "+" anyway.

public class MyClass
{
    readonly int[] _cs = new int[n];

    public int[] Cs { get { return _cs; } }

    public int C1 { get { return Cs[0]; } set { Cs[0] = value; } }
    public int C2 { get { return Cs[1]; } set { Cs[1] = value; } }
    public int C3 { get { return Cs[2]; } set { Cs[2] = value; } }
    .
    .
    .
    public int Cn { get { return Cs[n-1]; } set { Cs[n-1] = value; } }
}

Now you can use Enumerable.Sum with MyClass.Cs , and you can still map C1 , C2 , ... to database fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM