C#中的委託

來源:互聯網
上載者:User

理解一下委託

Code
using System;

namespace guoxinghua
{
    public delegate int MathOptDelegate(int value1,int value2);

    class Class
    {
public int Add(int argument1,int argument2)
        {
return argument1+argument2;
}
    }
    class Program
    {
        Static void Main(String[] args)
        {
        MathOptDelegate oppDel;
        MathOpt obj=new MathOpt();
        oppDel=obj.Add;//給此委託類型賦值
        Console.WriteLine(oppDel(1,2));//3
        }
    }
    
}    

 

 

 

 

 

 

Code
using System;
using System.Collections.Generic;
using System.Text;

namespace guoxinghua
{
    public delegate int MathOptDelegate(int value1,int value2);

    class Class
    {
        public static void Main(String[] args)
        {
            MathOpt myopt=new MathOpt();
            myopt.Add(1,2);
            MathOpt oppDel;
            oppDel=myopt.Add;
            oppDel(1,2);

            Console.WriteLine(PrintResult(MathOpt.Add,1,2));
        }
        public static int PrintResult(MathOptDelegate action,int a,int b)
        {
        int result=action(1,2);
        return result;}
    }
    public class MathOpt
    {
        public static int Add(int arg1,int arg2){
        return arg1+arg2;
        }
        public static int Sub(int arg1,int arg2){
        return arg1-arg2;
        }
    }
    
    
}    

 

 

 

 

Code
using System;

namespace guoxinghua
{
    delegate double DoubleOp(double x);
    class MathOperations
    {
        public static double multi(double value){
        return value*2;
        }
        public static double Square(double value)
        {
        return value*value;
        }
    }
    class MainEntryPoint
    {
        public static void Main(String[] args)
        {
            DoubleOp[] operations={
            new DoubleOp(MathOperations.MultiByTwo),new DoubleOp(MathOperations.Square)
                }
            
        for(int i=0;i<operation.length;i++)
        {
        Console.WriteLine("Using operations[{0}]",i);
        ProcessAndDisplayNumber(operation[i],2.0);
        Console.WriteLine();
        }
    }
    public static void ProcessAndDisplayNumber(DoubleOp action,double value)
    {
    double result=action(value);
    Console.WriteLine("Value is {0},result of operation is {1}",value,result);
    }
    
}
}

 

 

 

Code
using System;

namespace guoxinghua
{
    public delegate void Del(String testString)
    class MulticastDelegate
    {
        public static void Main(String[] args)
        {
            MessageTest OK=new MessageTest();
            Del d1=new Del(OK.Message1);
            Del d2=new Del(OK.Message2);
            Del d3=new Del(OK.Message3);
            Del dAll=d1+d2+d3;
            dAll("代理之廣播");
            dAll=aAll-d1;
            dAll("代理之多路委託");
            }
    }
    class MessageTest
    {
        public void Message1(String testString){
        Console.WriteLine("使用message1"+testString);
        }
        public void Message2(String testString)
        {
        Console.WriteLine("使用message2"+testString);
        }
        public void Message3(String testString)
        {
        Console.WriteLine("使用message3"+testString);
        }
    }
    
}    

 

 

 

 

C#2.0的匿名方法

 

 

Code
using System;

namespace guoxinghua
{
    class Class
    {
        public delegate void MyDelegate(int x);
        public static void Main(String[] args)
        {
            
            MyDelegate md;
            md=SomeMethod;
            md(100);
            Console.WriteLine("guoxinghua");
        }
        static void SomeMethod(int x)
        {
        Console.WriteLine(x);
        }
    }
    
}    

 

 

 

Code
using System;

namespace guoxinghua
{
    public delegate void MyDelegate(int x);
    class Class
    {
        public static void Main(String[] args)
        {
            MyDelegate md=delegate(int x){
            Console.WriteLine(x);
        }
            
        }
    }
    
}    

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.