What are Ref local variables and Ref return values ​​in C# 7.0?

PHPz
Release: 2023-09-11 22:37:02
forward
794 people have browsed it

C# 7.0 中的 Ref 局部变量和 Ref 返回值是什么?

Reference return values ​​allow methods to return a reference to a variable instead of than a value.

The caller can choose to treat the returned variable as value or reference.

The caller can create a new variable, which itself is a reference to the return value, called ref local.

In the example below, even if we modify the color it has no effect Raw array colors

Example

class Program{
   public static void Main(){
      var colors = new[] { "blue", "green", "yellow", "orange", "pink" };
      string color = colors[3];
      color = "Magenta";
      System.Console.WriteLine(String.Join(" ", colors));
      Console.ReadLine();
   }
}
Copy after login

Output

blue green yellow orange pink
Copy after login

To achieve this we can use ref locals

Example

public static void Main(){
   var colors = new[] { "blue", "green", "yellow", "orange", "pink" };
   ref string color = ref colors[3];
   color = "Magenta";
   System.Console.WriteLine(String.Join(" ", colors));
   Console.ReadLine();
}
Copy after login

Output

blue green yellow Magenta pink
Copy after login
Copy after login

Ref returns -

In the example below, even if we modify the color, it will not have any impact Original array color

Example

class Program{
   public static void Main(){
      var colors = new[] { "blue", "green", "yellow", "orange", "pink" };
      string color = GetColor(colors, 3);
      color = "Magenta";
      System.Console.WriteLine(String.Join(" ", colors));
      Console.ReadLine();
   }
   public static string GetColor(string[] col, int index){
      return col[index];
   }
}
Copy after login

Output

blue green yellow orange pink

Example

class Program{
   public static void Main(){
      var colors = new[] { "blue", "green", "yellow", "orange", "pink" };
      ref string color = ref GetColor(colors, 3);
      color = "Magenta";
      System.Console.WriteLine(String.Join(" ", colors));
      Console.ReadLine();
   }
   public static ref string GetColor(string[] col, int index){
      return ref col[index];
   }
}
Copy after login

Output

blue green yellow Magenta pink
Copy after login
Copy after login

The above is the detailed content of What are Ref local variables and Ref return values ​​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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!