Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีการรวมใน C #


ใช้เมธอด Aggregate ใน C# เพื่อดำเนินการทางคณิตศาสตร์ เช่น Sum, Min, Max, Average เป็นต้น

เรามาดูตัวอย่างการคูณองค์ประกอบอาร์เรย์โดยใช้วิธีรวม

นี่คืออาร์เรย์ของเรา -

int[] arr = { 10, 15, 20 };

ตอนนี้ ใช้วิธี Aggregate() -

arr.Aggregate((x, y) => x * y);

นี่คือรหัสที่สมบูรณ์ -

ตัวอย่าง

using System;
using System.Linq;
using System.IO;
public class Demo {
   public static void Main() {
      int[] arr = { 10, 15, 20 };
      // Multiplication
      int res = arr.Aggregate((x, y) => x * y);
      Console.WriteLine(res);
   }
}

ผลลัพธ์

3000