C#_delegate - 值参数和引用参数

时间:2022-03-23 18:06:45

值参数不能加,引用参数可以。

引用参数是共享的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Delegate
{
public delegate void Myfunction(int valCount,ref int refCount); class Program
{ static void Main(string[] args)
{ Myfunction mf = (Myfunction)MethodA + (Myfunction)MethodB + (Myfunction)MethodA; int valCount = 0;
int refCount = 0; mf(valCount,ref refCount); //display 0;
Console.WriteLine("valCount: "+valCount);
//display 5;
Console.WriteLine("refCount: "+refCount); Console.ReadLine();
} public static void MethodA(int valCount, ref int refCount)
{
++valCount;
++refCount;
}
public static void MethodB(int valCount, ref int refCount)
{
valCount += 3;
refCount += 3;
} }
}