C# 按值调用
C# 按值调用
在 C# 中,值类型参数是将原始值的副本传递给函数而不是引用。它不会修改原始值。对传递值所做的更改不会改变实际值。在下面的示例中,我们在函数调用期间传递值。
C# 按值调用示例
using System;
namespace CallByValue
{
class Program
{
// User defined function
public void Show(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(val); // Calling Function by passing value
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 50