Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
当前位置: 主页 > vs2010教程 >

Visual Studio2010讲解C#语法(6)--Delegates

时间:2010-08-06 18:45来源:未知 作者:admin 点击:

介绍
我相信大多数人谁想要学习C#为C / C + +程序员。因此,我相信他们将在C#的特点是寻找一些类似的C / C + +功能,他们很喜欢。与旧的C约好我最喜欢的特点之一是函数指针。你们当中谁的天堂@#T中使用函数指针错过了乐趣。 C#中确实有好东西,可以用来在我们过去经常使用函数指针。其实他们做了很多多用来做函数指针。他们被称为代表。正如我往常一样,我会尽力和代表们展示了通过使用评论,抽样方案,体积小,简单,希望容易理解。

程序1
在这个程序中我们会看到如何@#代表用于封装一提到在一个委托对象的方法。正如你可以看到我们可以声明一个命名空间中的代表,因此代表们中间阶层共享。您还可以看到,我们可以关联一个静态成员函数和实例变量代表。

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> 

(责任编辑:admin)

Tags:vs2010 C#4.0
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价