Kullanım örnekleri:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; int sum = numbers.Sum(); // sum = 15
- Seçici ile kullanım: Bir sınıf içindeki özelliklerin toplamını hesaplamak için 2:
List<Fruit> fruits = new List<Fruit>() { new Fruit() { Name = "Orange", Price = 4.5M }, new Fruit() { Name = "Pear", Price = 2.9M }, ... }; decimal totalPrice = fruits.Sum(x => x.Price); // totalPrice = 14.0
Bu örnekte,
Sumyöntemi
x.Priceifadesini seçici olarak kullanır ve her bir meyvenin fiyatını toplar 2.
5 kaynaktan alınan bilgiyle göre: