C#通过值传递参数示例

class cbv
    {
        static void squarit(int x)
        {
            x *= x;
            System.Console.WriteLine("the value inside the method:{0}", x);
        }
        static void Main()
        {
            int n = 5;
            System.Console.WriteLine("the value before calling method:{0}",n);
            squarit(n);
            System.Console.WriteLine("the value after calling methoid:{0}:",n);
            System.Console.WriteLine("presss any key to exit:");
            System.Console.ReadKey();
        }
    }
}

C# 通过引用传递参数示例

在函数内修改变量值,会影响函数外相同名称的变量值。(因为它们指向同一个地址,即其实为同一变量)

class cbv
    {
        static void squarit(ref  int x)
        {
            x *= x;
            System.Console.WriteLine("the value inside the method:{0}", x);
        }
        static void Main()
        {
            int n=5;
            System.Console.WriteLine("The value inside the method:{0}",n);
            squarit(ref n);

            System.Console.WriteLine("The value after calling the method:{0}",n);

            System.Console.WriteLine("presss any key to exit:");
            System.Console.ReadKey();
        }
    }
}
C#通过值传参和通过引用传参的区别

在通过值传递参数中,变量是在调用函数之前定义的。

日期:2020-04-11 23:03:47 来源:oir作者:oir