c# ref 的作用

时间:2022-01-24 02:03:43

Usage of ref keyword in C# 

When we pass a value type variable as a parameter, then it passes its value as a parameter i.e. making a copy of the data. Making any changes inside the method in which it has been passed as a parameter will not affect the variable accessed outside the method. Because it is passed as a value and whatever the changes will be made inside the local method will be done on a copy of that variable.

If we need to change the value type variable from inside the method in which it has been passed as parameter, then you can pass this value type variable as the reference, using the ‘ref’ keyword. In this case, the reference of value type will be passed as a parameter rather than value of the variable