Home  >  Article  >  Backend Development  >  What improvements have been made to Out parameters in C# 7.0?

What improvements have been made to Out parameters in C# 7.0?

王林
王林forward
2023-08-22 14:49:021290browse

C# 7.0中的Out参数有哪些改进?

We can declare values ​​inline as parameters for methods.

Now, the existing out parameters have been improved in this version. Now we can declare

Use out variables in the argument list of a method call instead of writing separate code Declaration statement.

Advantages

  • The code is more readable.

  • No need to assign an initial value.

Existing syntax

Example

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();
   }
}

Output

15
50

New syntax

Example

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();
   }
}

Output

15
50

The above is the detailed content of What improvements have been made to Out parameters in C# 7.0?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete