个性化阅读
专注于IT技术分析

C#引用调用函数

C#提供了ref关键字以将参数作为引用类型传递。它将参数引用传递给函数,而不是原始值的副本。传递的值的更改是永久性的,并且会修改原始变量值。

C#调用参考示例

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
赞(0)
未经允许不得转载:srcmini » C#引用调用函数

评论 抢沙发

评论前必须登录!