C# 按引用调用
C# 通过引用调用
C# 提供了一个 ref 关键字来将参数作为引用类型传递。它将参数的引用传递给函数,而不是原始值的副本。传递值的更改是永久性的,并且修改原始变量值。
C# Call By Reference Example
using System;
namespace CallByReference
{
class Program
{
// User defined function
public void Show(ref int val)
{
val *= val; // Manipulating value
Console.WriteLine("Value inside the show function "+val);
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args)
{
int val = 50;
Program program = new Program(); // Creating Object
Console.WriteLine("Value before calling the function "+val);
program.Show(ref val); // Calling Function by passing reference
Console.WriteLine("Value after calling the function " + val);
}
}
}
输出:
Value before calling the function 50
Value inside the show function 2500
Value after calling the function 2500