C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C# 按引用调用

C# 通过引用调用

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

C# Call By Reference Example

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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4