当前位置:网站首页>C method parameter: ref
C method parameter: ref
2022-06-13 03:07:00 【cathy18c】
class Program
{
static void Add(int num1)
{
num1++;
Console.WriteLine("num1 The value of is :{0}", num1);
}
static void Main(string[] args)
{
int num1 = 10;
Add(num1);
Console.WriteLine("num1 The value of is :{0}",num1);
}
}
The final output :
Add In the way num1 The value of is :11
Main In the way num1 The value of is :10
stay Add The method is internal to num1 The modification of does not affect the num1
When a value type is passed as an argument , You pass a copy of the value , Parameter received this value , Changing this value does not change the argument itself . But if we have a requirement to change this argument , And you can't use reference types , That is to say, we should pass the value type as an argument and change the value , Then use ref keyword .
void Method(ref int refArgument)
{
refArgument = refArgument + 44;
}
int number = 1;
Method(ref number);
Console.WriteLine(number);
// Output: 45
Just look at this example , The argument passes the value type to Method Method ,Method This value has been modified internally in the method , There is no need to return sentence , To it number Also changed. . This is it. ref Credit .ref The value type is passed by reference , Instead of changing a value type into a reference type
If you use ref keyword , The definition and call of that method should use ref, For example, the definition of the above example method void Method(ref int refArgument) Method call Method(ref number) With the ref
Pass on to ref Arguments to must initialize values , Take the example above , If number Not initialized to 1, It just defines int number; That's wrong , I can't compile , Tips “ An unassigned local variable is used number”
On overloading methods , Look at the code below
class CS0663_Example
{
// Compiler error CS0663: "Cannot define overloaded
// methods that differ only on ref and out".
public void SampleMethod(out int i) { }
public void SampleMethod(ref int i) { }
}
Class members Cannot have only in ref、in or out Different signatures . If the only difference between two members of a type is that one of them has ref Parameters , And the other has out or in Parameters , A compiler error occurs . Everything else is the same , just ref out in Different , Not enough to prove that there are two different approaches .
The following is right
class RefOverloadExample
{
public void SampleMethod(int i) { }
public void SampleMethod(ref int i) { }
}
ref keyword , Initialization is required when variables are passed in , The value can be updated inside the method , And automatically return to the arguments that call it , Update the value of the argument
ref Address / reference , This parameter must have been initialized when calling
Reference resources : Reference type and value type , And reference passing and value passing
Method parameter (C# Reference resources )
边栏推荐
- Typical application of ACL
- How to become a technological bull -- from the bull man
- Keil去掉烦人的ST-Link更新提示
- HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
- Sword finger offer2: queue summary
- Using binary heap to implement priority queue
- Entity framework extends the actual combat, small project reconfiguration, no trouble
- Hash table: least recently used cache
- JS deconstruction assignment
- Vs 2022 new features_ What's new in visual studio2022
猜你喜欢
Prometheus node_exporter安装并注册为服务
Data warehouse notes | 5 factors that need attention for customer dimension modeling
Linked list: the first coincident node of two linked lists
JVM class loading (I)
專業的數據庫管理軟件:Valentina Studio Pro for Mac
Introduction and download of common data sets for in-depth learning (with network disk link)
Applet image component long press to identify supported codes
Es and kibana deployment and setup
JVM virtual machine stack (III)
Mvcc and bufferpool (VI)
随机推荐
Install MySQL database
MySQL index optimization (4)
How to manage the IT R & D department?
Review notes of RS data communication foundation STP
遍历数组,删除某元素,直到删除为止
通过Web ETL统一调度和管理DataX任务
Six special GPU products for domestic aircraft passed the appraisal and review
Operating principle of JS core EventLoop
二叉树初始化代码
Prometheus node_exporter安装并注册为服务
Linked list: orderly circular linked list
开源-校园论坛和资源共享小程序
Image classification system based on support vector machine (Matlab GUI interface version)
JS deconstruction assignment
Wechat applet coordinate location interface usage (II) map interface
Ijkplayer source code --- decode
Open source - campus forum and resource sharing applet
Ijkplayer source code -- Library loading and initialization
MySQL transactions and locks (V)
Hash table: the time complexity of insert, delete and random access is O (1)