最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Delegates in C# - an introduction(转:英文)
时间:2022-07-02 18:03:21 编辑:袖梨 来源:一聚教程网
Introduction
I assume that most people who want to learn C# are C/C++ programmers. Thus I believe that they will be looking for features in C# which are analogous to some C/C++ feature they were quite fond of. And one of my favorite features about good old C was function pointers. Those of you who haven't used function pointers missed out on the fun. Well C# does have something that can be used where we used to use function pointers. Actually they do a lot more than function pointers used to do. They are called delegates. As is usual with me, I'll try and demonstrate the use of delegates through commented, sample programs that are small, simple and hopefully easy to understand.
Program 1
In this program we'll see how delegates are used to encapsulate a reference to a method in a delegate object. As you can see we can declare delegates in a namespace and thus delegates are shareable among classes. You can also see that we can associate a delegate variable with both static and instance member functions.
using System;
//declare our delegate type
public delegate void dgate();
class nish
{
public static void Main()
{
test t1 = new test();
//create a new delegate object and associate it
//with the function abc in class test
dgate d1 = new dgate(t1.abc);
d1(); //call the delegate
//now associate the delegate object with
//the function xyz in class test
d1 = new dgate(t1.xyz);
d1(); //call the delegate
I assume that most people who want to learn C# are C/C++ programmers. Thus I believe that they will be looking for features in C# which are analogous to some C/C++ feature they were quite fond of. And one of my favorite features about good old C was function pointers. Those of you who haven't used function pointers missed out on the fun. Well C# does have something that can be used where we used to use function pointers. Actually they do a lot more than function pointers used to do. They are called delegates. As is usual with me, I'll try and demonstrate the use of delegates through commented, sample programs that are small, simple and hopefully easy to understand.
Program 1
In this program we'll see how delegates are used to encapsulate a reference to a method in a delegate object. As you can see we can declare delegates in a namespace and thus delegates are shareable among classes. You can also see that we can associate a delegate variable with both static and instance member functions.
using System;
//declare our delegate type
public delegate void dgate();
class nish
{
public static void Main()
{
test t1 = new test();
//create a new delegate object and associate it
//with the function abc in class test
dgate d1 = new dgate(t1.abc);
d1(); //call the delegate
//now associate the delegate object with
//the function xyz in class test
d1 = new dgate(t1.xyz);
d1(); //call the delegate
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21