Gibt es eine elegantere Möglichkeit, NULL-fähige Ints hinzuzufügen?

Muss ich hinzufügen, eine Vielzahl von Variablen vom Typ nullable int. Ich verwendet den null-coalescing operator, um es bis auf eine variable pro Zeile, aber ich habe das Gefühl, es ist ein präziser Weg, dies zu tun, z.B. kann ich keine Kette, diese Aussagen irgendwie zusammen, ich habe gesehen, dass zuvor in der anderen code.

using System;

namespace TestNullInts
{
    class Program
    {
        static void Main(string[] args)
        {
            int? sum1 = 1;
            int? sum2 = null;
            int? sum3 = 3;

            //int total = sum1 + sum2 + sum3;
            //int total = sum1.Value + sum2.Value + sum3.Value;

            int total = 0;
            total = total + sum1 ?? total;
            total = total + sum2 ?? total;
            total = total + sum3 ?? total;

            Console.WriteLine(total);
            Console.ReadLine();
        }
    }
}

InformationsquelleAutor der Frage Edward Tanguay | 2010-08-30

Schreibe einen Kommentar