您的位置:首页 > 编程语言 > C#

C#高级------委托

2015-09-11 12:05 501 查看
namespace out_ref
{
//声明一个委托
public delegate void MyDelegate();
class Program
{
static void Main(string[] args)
{
MyDelegate mdl = Say;
Do(mdl);
Console.ReadKey();
}

static void Do(MyDelegate mdl)
{
mdl();
}

static void Say()
{
Console.WriteLine("哈哈");
}

}
}


namespace out_ref
{
//声明一个委托
public delegate int MyAdd(int n1,int n2);
class Program
{
static void Main(string[] args)
{
Do(Sum);
Console.ReadKey();
}

static void Do(MyAdd myadd)
{
int sum = myadd(1,3);
Console.WriteLine(sum);
}

static int Sum(int n1,int n2)
{
return n1 + n2;
}

}
}


namespace out_ref
{
//委托占位案例
public delegate void Myadd();
class Program
{
static void Main(string[] args)
{
Do(Dafeiji);
Console.ReadKey();
}

static void Do(Myadd myadd)
{
Console.WriteLine("我正在吃饭....");
myadd();
Console.WriteLine("我正在睡觉....");
}

static void Play()
{
Console.WriteLine("我在玩游戏....");
}

static void Dafeiji()
{
Console.WriteLine("我正在打飞机....");
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: