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

การปรับปรุง Out Parameter ใน C # 7.0 คืออะไร


เราสามารถประกาศค่าแบบอินไลน์เป็นอาร์กิวเมนต์ของวิธีการที่ใช้

พารามิเตอร์ขาออกที่มีอยู่ได้รับการปรับปรุงในเวอร์ชันนี้ ตอนนี้ เราสามารถประกาศตัวแปรในรายการอาร์กิวเมนต์ของการเรียกเมธอด แทนที่จะเขียนคำสั่งแยกจากกัน

ข้อดี

  • รหัสนี้อ่านง่ายกว่า

  • ไม่จำเป็นต้องกำหนดค่าเริ่มต้น

ไวยากรณ์ที่มีอยู่

ตัวอย่าง

class Program{
   public static void AddMultiplyValues(int a, int b, out int c, out int d){
      c = a + b;
      d = a * b;
   }
   public static void Main(){
      int c;
      int d;
      AddMultiplyValues(5, 10, out c, out d);
      System.Console.WriteLine(c);
      System.Console.WriteLine(d);
      Console.ReadLine();
   }
}

ผลลัพธ์

15
50

ไวยากรณ์ใหม่

ตัวอย่าง

class Program{
   public static void AddMultiplyValues(int a, int b, out int c, out int d){
      c = a + b;
      d = a * b;
   }
   public static void Main(){
      AddMultiplyValues(5, 10, out int c, out int d);
      System.Console.WriteLine(c);
      System.Console.WriteLine(d);
      Console.ReadLine();
   }
}

ผลลัพธ์

15
50