在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
评论前必须登录!
注册