首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网络编程-> C#/CSHARP教程
Delegates in C# - an introduction(转:英文)-.NET教程,面向对象编程
作者:网友供稿 点击:47
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
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 havent 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, ill 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 well 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
        
        //here we associate the delegate with the static
        //function helloworld from this same class
        d1 = new dgate(helloworld);
        d1(); //call the delegate
    }
    public static void helloworld()
    {
        console.writeline("hello world");
    }    
}

class test
{
    public void abc()
    {
        console.writeline("this is test.abc()");
    }
    public void xyz()
    {
        console.writeline("this is test.xyz()");
    }
}

output

f:\c#\delegates>deleg01
this is test.abc()
this is test.xyz()
hello world

f:\c#\delegates>
program 2
this program will demonstrate how you can compose a delegate from two other delegates using the + operator and also how you can remove a component delegate from a composed delegate using the - operator. the output of the program should make things quite clear. a composed delegate can consist of one or more components of one or more delegates. thus, for example, you can have a composed delegate with three components, two which are both of the same delegate type.

using system;
public delegate void dgate(string s1);

class nish
{
    public static void main()
    {
        //declare four dgate delegates
        dgate a1,a2,a3,a4;
        
        a1=new dgate(morning);
        a2=new dgate(night);
        //composing a3 by adding a1 and a2
        a3=a1+a2;
        //removing a1 from the composed delegate
        a4=a3-a1;
        
        console.writeline("calling a1");
        a1("a1");
        console.writeline("\r\ncalling a2");
        a2("a2");
        console.writeline("\r\ncalling a3");
        a3("a3");
        console.writeline("\r\ncalling a4");
        a4("a4");
        
        //you can add the same delegate several times
        //and you can have the same delegate variable as lhs and rhs
        a1=a1+a1+a1;
        console.writeline("\r\ncalling a1");
        a1("a1");
    }
    public static void morning(string s1)
    {
        console.writeline("good morning "+s1);
    }
    public static void night(string s1)
    {
        console.writeline("good night "+s1);
    }    
}

output

f:\c#\delegates>deleg02
calling a1
good morning a1

calling a2
good night a2

calling a3
good morning a3
good night a3

calling a4
good night a4

calling a1
good morning a1
good morning a1
good morning a1

f:\c#\delegates>
program 3
in this program i will show you how you can pass a delegate object to a function. you can see how delegates are useful in splitting up functionality. the display() function has no idea, what function the delegate passed to it is referencing. thus we can have separate functional blocks all independent of each other.

using system;
public delegate int dgate(int i);
class nish
{
    public static void main()
    {
        //associate d1 to static function square
        dgate d1=new dgate(square);
        display(d1,"square");
        
        //associate d1 to static function cube
        d1=new dgate(cube);
        display(d1,"cube");
    }
    
    /* this function takes two parameters, a delegate of type dgate and a string */
    public static void display(dgate d1, string s)
    {
        console.writeline(s+" of 5 is "+d1(5));
    }
    
    public static int square(int y)
    {
        return y*y;
    }
    public static int cube(int y)
    {
        return y*y*y;
    }
}

output

f:\c#\delegates>deleg03
square of 5 is 25
cube of 5 is 125

f:\c#\delegates>

文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·使用C#编写Windows Forms应用程序(转)-.NET教程,Windows开发
·C#学习杂记-.NET教程,C#语言
·将文本格式的文章转换为html/xml格式文本的功能封装到Javabean-JSP教程,Java技巧及代码
·用java小程序applet实现无限级树结构-JSP教程,Java技巧及代码
·《.net编程先锋C#》第一章 C#简介-.NET教程,C#语言
·企业人事信息管理系统1.0-ASP教程,数据库相关
·《.net编程先锋C#》第三章 第一个C#应用程序-.NET教程,C#语言
·《.net编程先锋C#》第二章 理论基础-公用语言 运行环境-.NET教程,C#语言
·Windows应用程序调试必备的--符号文件(Symbols)-.NET教程,评论及其它
·c#反编译微软msdn2003的帮助文档,并将反编译结果保存到一个sqlserver数据库中
最新文章
·vista的新特性:懒人的安全感_windows vista
·photoshop为情侣照片打造韩式梦幻相框_photoshop教程
·专访seobbs站长乐思蜀_站长访谈
·嘟嘟网络陈艺光:诚信是一种信仰_站长访谈
·俺自己黄修源:只问耕耘,不问收获_站长访谈
·李向华:如何提高论坛用户粘性_站长访谈
·给windows vista系统网络和共享中心“换脸”_windows vista
·胡宪东谈搜索优化的实战_站长心得
·网站快速成功的九个步骤_站长心得
·google pr数字背后的含意_google推广
相关主题
  • delegate 与 多线程-.NET教程,算法/线程
  • Delegate的实际应用。-.NET教程,Windows开发
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭