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

王林
Release: 2023-08-22 14:49:02
forward
1294 people have browsed it

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

Output

15
50
Copy after login
Copy after login

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

Output

15
50
Copy after login
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template