委托转换,委托重载

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    delegate void MyAction();
    delegate void MyAction<T>(T obj);
    delegate void MyAction<T1, T2>(T1 who, T2 say);

    delegate string MyFunc();
    delegate T MyFunc<T>();
    delegate TResult MyFunc<T, TResult>(T give);
    delegate TResult MyFunc<T1, T2, TResult>(T1 give, T2 n);
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Action------------------");
            Action a = () => { Console.WriteLine("hello"); };
            a.Invoke();

            Action<string> b = x => { Console.WriteLine(x); };
            b("hi");

            Action<string, string> c = (x, y) => Console.WriteLine(x + " say " + y);
            c("Mr.Du", "Hello");

            Console.WriteLine("MyAction------------------");

            MyAction a1 = a.Method.CreateDelegate(typeof(MyAction)) as MyAction;
            a1();

            MyAction<string> b1 = b.Method.CreateDelegate(typeof(MyAction<string>)) as MyAction<string>;
            b1("hi1");

            MyAction<string, string> c1 = c.Method.CreateDelegate(typeof(MyAction<string, string>)) as MyAction<string, string>;
            c1("Mr.Du", "Hello1");

            Console.WriteLine("Func------------------");

            Func<string> f1 = () => { return "f1"; };
            Console.WriteLine(f1());

            Func<string, string> f2 = x => { return x + x; };
            string give = "a";
            Console.WriteLine(f2(give) + " get of " + give);

            Func<string, int, string> f3 = (x, n) =>
            {
                for (int i = 0; i < n; i++)
                {
                    x += x;
                }
                return x;
            };
            Console.WriteLine(f3(give, 3) + " get of " + give);

            Console.WriteLine("MyFunc------------------");

            MyFunc mf1 = f1.Method.CreateDelegate(typeof(MyFunc)) as MyFunc;
            Console.WriteLine(mf1());

            MyFunc<string> mf2 = f1.Method.CreateDelegate(typeof(MyFunc<string>)) as MyFunc<string>;
            Console.WriteLine(mf2());

            MyFunc<string, string> mf3 = f2.Method.CreateDelegate(typeof(MyFunc<string, string>)) as MyFunc<string, string>;
            Console.WriteLine(mf3(give) + " get of " + give);

            MyFunc<string, int, string> mf4 = f3.Method.CreateDelegate(typeof(MyFunc<string, int, string>)) as MyFunc<string, int, string>;
            Console.WriteLine(mf4(give,3) + " get of " + give);
        }
    }
}

时间: 2024-08-11 01:35:10

委托转换,委托重载的相关文章

C# 委托之把委托从委托链(多播委托)移除

运用“-”运算符将委托从委托链移除 1 class HelloWorld 2 { 3 //定义委托类型 4 delegate void DelegationChain(); 5 static void Main(string[] args) 6 { 7 //用静态方法来实例委托 8 DelegationChain mydelegateone=new DelegationChain(HelloWorld.Fun2); 9 //用实例方法来实例委托 10 DelegationChain mydele

09.C#委托转换和匿名方法(五章5.1-5.4)

今天将书中看的,自己想的写出来,供大家参考,不足之处请指正.进入正题. 在C#1中开发web form常常会遇到使用事件,为每个事件创建一个事件处理方法,在将方法赋予给事件中,会使用new EventHandler(),不同的事件有各种不同的EventHandler的派生类的实例,因为我这里使用的时Console App,原理是一样的,且看 //定义一个委托 delegate void Printer(); static void Main(string[] args) { Printer p

多种方法求最大值(委托方法,重载)

1 namespace ConsoleAppLearningCeshi 2 { 3 /// <summary> 4 /// 不同打招呼 5 /// </summary> 6 /// <param name="name"></param> 7 public delegate int deleMaxCompara<T>(T one, T two);//泛型委托 8 public delegate int deleMaxCompar

委托, 泛型委托,Func&lt;T&gt;和Action&lt;T&gt;

使用委托来做一些事情,大致思路是: 1.定义声明一个委托,规定输入参数和输出类型.2.写几个符合委托定义的方法.3.把方法列表赋值给委托4.执行委托 internal delegate int MyDelegate(); class Program { static void Main(string[] args) { MyDelegate d = ReturnOne; d += ReturnTwo; foreach (int i in GetAllReturnVals(d)) { Consol

初识C#委托及委托链

委托是c#很重要的特性.代码如下: class Program { public delegate void DelegateTest(); static void Main(string[] args) { DelegateTest dtstatic = new DelegateTest(Program.method1);//实例化,静态方法,不用new DelegateTest dtinstance = new DelegateTest(new Program().method2);//实例

委托学习笔记后续:泛型委托及委托中所涉及到匿名方法、Lambda表达式

引言: 最初学习c#时,感觉委托.事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托.事件学习笔记.今天重新温故委托.事件,并且把最近学习到和委托相关的匿名方法.Lambda表达式及泛型委托记录下来,以备复习使用. 委托: 日常工作中,常常见到委托用在具体的项目中.而且委托使用起来相对来说也是非常简单的,下面列举一个委托实例用以说明如何使用委托,代码如下: class Program { public delegate int CalculateDelegate(int x, int y)

【C#进阶】多播委托和委托数组像是一回事~

这个MathOperation类有三静态方法,参数都是double,并且没有返回值,考虑用Action<>() 这种预定义的委托哦 class MathOperations { public static void MultiplyByTwo(double value) { Console.WriteLine( "2* {0} = {1}",value,value * 2); } public static void Square(double value) { Conso

分配委托、匿名委托、委托

分配委托(将命名方法分配给其委托) using System; public class GenericFunc { public static void Main() { // Instantiate delegate to reference UppercaseString method Func<string, string> convertMethod = UppercaseString; string name = "Dakota"; // Use delegat

C#委托一——委托初解

PanPen120在CSDN上原创,如其他网站转载请注意排版和写明出处: 今天一直在研究委托,因为有函数指针的基础,还容易上手,但是对于一些概念和实践,总是为了弄的非常清楚而纠结,这几篇关于委托的文章我是结合<C#与.NET4高级程序设计>.MSDN.借鉴其他人的博文来总结话语,以最直接简洁的话来阐述清楚(在我弄懂之前网上的感觉都很模糊) namespace DelegateDemo { //function1: public delegate int setMyName(string nam