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

介绍
我相信大多数人谁想要学习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>

程序2
这一计划将演示如何从其他两个代表组成一个代表使用+运算符,以及如何你可以删除一个组成使用 - 运营商委派代表组成。该方案应使事情很清楚的输出。甲组成代表可以由一个或多个代表一个或多个组件。因此,例如,你可以有一个由三个部门组成,其中两名代表组成的两个相同的委托类型。

 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>

 程序3
在这个项目中我将告诉你如何能够通过一个委托对象的功能。你可以看到代表们在分裂的功能非常有用。显示()函数不知道什么函数传递给它的委托引用。因此,我们可以有独立的所有相互独立的功能模块。

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>

你可能感兴趣的:(String,function,C#,Class,2010,delegates)