在C#中,值类型参数是将原始值的副本传递给函数,而不是引用。它不会修改原始值。对传递的值所做的更改不会改变实际值。在下面的示例中,无涯教程在函数调用期间传递了值。
using System; namespace CallByValue { class Program { // 用户定义的功能 public void Show(int val) { val *= val; Console.WriteLine("Value inside the show function "+val); // No return statement } // 主函数,程序的执行入口点 static void Main(string[] args) { int val = 50; Program program = new Program(); // 创建对象 Console.WriteLine("Value before calling the function "+val); program.Show(val); //将值当作参数传入函数 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
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)