1024programmer Asp.Net ref keyword in C#

ref keyword in C#

ref keyword in C#

The public account “DotNet Learning Exchange” shares the details of learning DotNet. This article briefly introduces the ref keyword in C# and gives sample code.

Value Parameters

1. What is passed is the value of the parameter (a copy of the data) rather than the original data itself.

2. Modification of value parameters within the function will not affect the original data.

3. Usually used to transfer basic data types (such as integers, floating point numbers, Boolean values) or immutable objects (such as string, tuple).

4. The transfer of value parameters is a call by value (Call by Value).

Sample code

 internal class Program
{
static void ShowDouble(int val)
                                                                       ">val *= 2;
                                         cm-variable">WriteLine($"The value of val in the ShowDouble function is: {val}");
}
static void Main(string[] args)
                                                                    variable">val = 6;
                                                                                                    cm-variable">WriteLine($"The value of val before executing the ShowDouble function in the Main function is: {val}");
                                                                                                                                                                                 "cm-variable">WriteLine($"The value of val after executing the ShowDouble function in the Main function is: {val}");
}
}

The running results are as shown below:

image-20231030185024616

Using val as a parameter, calling ShowDouble() does not affect the value of val in Main()

So how to change the value of val?

You can use a function with a return value

The code is as follows:

 internal class Program
{
static int ShowDouble(int val)
                                                                       -variable">val *= 2;
                                                                                                  ="cm-variable">WriteLine($"The value of val in the ShowDouble function is: {val}");
return val;
}
  static void Main(string[ ] args)
{
int val = 6;
                                                              >WriteLine($"The value of val before executing the ShowDouble function in the Main function is: {val}");
span class="cm-variable">val = ShowDouble(val);
           Console.WriteLine($"Main function The value of val after executing the ShowDouble function is: {val}");
}
}

The running results are as follows:

image-20231030185402536

The value of the parameter was successfully changed, but this method is not intuitive and cannot change multiple variables used as parameters. value (because the function has only one return value), then the parameters can be passed by reference.

Reference Parameters

1. What is passed is the reference (memory address) of the parameter, and the original data is directly manipulated.

2. Modification of reference parameters within the function will affect the original data.

3. Usually used to pass mutable objects (such as lists, dictionaries) or object instances for modification inside the function And maintain the modified state outside the function.

4. The passing of reference parameters is a call by reference.

Use the ref keyword to specify parameters in C#, the code is as follows:

 internal class Program
{
static void ShowDouble(ref int val)
{
                                                                                                                                          -variable">Console.WriteLine($"The value of val in the ShowDouble function is: {val }");
}
static void Main (string[] args)
                                                                                                      3">int val = 6;
                                                                                    -variable">Console.WriteLine($"The value of val before executing the ShowDouble function in the Main function is: {val}");
                                                                                         
                                                                                                                                                                                                        >"The value of val after executing the ShowDouble function in the Main function is: {val}");
}
}

The running results are as shown below:

image-20231030190228869

It should be noted that ref is not only used to specify parameters in the function declaration, such as the code static void ShowDouble(ref int val) As shown, in the function call, it must be specified again, as shown in the code ShowDouble(ref val).

Restrictions on using ref

1. The function may change the value of the reference parameter, so “non-const” variables must be used in the function call

 const int val = 6;

With this writing method, the compiler will report an error, as shown in the following figure:

image-20231030191147516

2. Initialized variables must be used. C# does not allow you to assume that a ref parameter is initialized in the function that uses it.

 int val;
ShowDouble(ref val);

With this writing method, the compiler will also report an error, as shown in the following figure:

image-20231030191535799

Reference books

“C# Introductory Classic (7th Edition)”——Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

/span>

The running results are as shown below:

image-20231030190228869

It should be noted that ref is not only used to specify parameters in the function declaration, such as the code static void ShowDouble(ref int val) As shown, in the function call, it must be specified again, as shown in the code ShowDouble(ref val).

Restrictions on using ref

1. The function may change the value of the reference parameter, so “non-const” variables must be used in the function call

 const int val = 6;

With this writing method, the compiler will report an error, as shown in the following figure:

image-20231030191147516

2. Initialized variables must be used. C# does not allow you to assume that a ref parameter is initialized in the function that uses it.

 int val;
ShowDouble(ref val);

With this writing method, the compiler will also report an error, as shown in the following figure:

image-20231030191535799

Reference books

“C# Introductory Classic (7th Edition)”——Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/811198

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索