亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#中的多播委托和泛型委托

 更新時間:2022年05月04日 16:25:59   作者:農(nóng)碼一生  
這篇文章介紹了C#中的多播委托和泛型委托,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

多播委托

簡介

  • 每一個委托都是繼承自MulticastDelegate,也就是每個都是多播委托。
  • 帶返回值的多播委托只返回最后一個方法的值
  • 多播委托可以用加減號來操作方法的增加或者減少。
  • 給委托傳遞相同方法時 生成的委托實例也是相同的(也就是同一個委托)

代碼實現(xiàn)

 	//聲明委托
    delegate void MulticastTest();
    public class MulticastDelegateTest
    {
        
            
        public void Show()
        {
            MulticastTest multicastTest = new MulticastTest(MethodTest);
            multicastTest();



            Action action =new Action(MethodTest);
            action = (Action)MulticastDelegate.Combine(action, new Action(MethodTest2));
            action = (Action)MulticastDelegate.Combine(action, new Action(MethodTest3));
            action = (Action)MulticastDelegate.Remove(action, new Action(MethodTest3));
            action();

            //等同于上面
            action = MethodTest;
            action += MethodTest2;
            action += MethodTest3;
            action -= MethodTest3;

            foreach (Action action1 in action.GetInvocationList())
            {
                action1();
            }
            Console.WriteLine("==========");
            action();
                        


            Func<string> func = () => { return "我是Lambda"; };
            func += () => { return "我是func1"; };
            func += () => { return "我是func2"; };
            func += GetTest;
            func += GetTest; //給委托傳遞相同方法時 生成的委托實例也是相同的(也就是同一個委托)
            
            string result = func();
            Console.WriteLine(result);
            Console.WriteLine("==========");
        }


        #region 委托方法
        public void MethodTest()
        {
            Console.WriteLine("我是方法MethodTest()1");
        }

        public void MethodTest2()
        {
            Console.WriteLine("我是方法MethodTest()2");
        }

        public void MethodTest3()
        {
            Console.WriteLine("我是方法MethodTest()3");
        }

        public string GetTest()
        {
            return "我是方法GetTest()";
        }
        #endregion
    }

泛型委托

代碼實現(xiàn)

    //泛型委托聲明
    delegate void GenericDelegate<T>(T t);
    public class GenericDelegate
    {
        public static void InvokeDelegate()
        {
            GenericDelegate<string> genericDelegate = new GenericDelegate<string>(Method1);
            genericDelegate("我是泛型委托1");

            //官方版本(不帶返回值)
            Action<string> action = new Action<string>(Method1);
            action("我是泛型委托1");
            //Action<string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string>

            GenericDelegate<int> genericDelegate1 = new GenericDelegate<int>(Method2);
            genericDelegate1(2);

            //官方版本(帶回值)
            Func<string, string> func = new Func<string, string>(Method3);
            string ret = func("我是帶返回值Func委托");
            Console.WriteLine( ret );
            //Func<string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string,string>
        }



        #region 委托方法

        public static void Method1(string str)
        {
            Console.WriteLine(str);
        }

        public static void Method2(int num)
        {
            Console.WriteLine("我是泛型委托2 "+num);
        }

        public static string Method3(string str )
        {
            return str;
        }

        #endregion
    }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)

    C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)

    下面小編就為大家?guī)硪黄狢# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • C#打印日志的方法總結(jié)

    C#打印日志的方法總結(jié)

    在本篇文章里小編給大家整理了關(guān)于C#如何打印日志的技巧總結(jié),需要的朋友們跟著學(xué)習(xí)下。
    2019-03-03
  • c#文本加密程序代碼示例

    c#文本加密程序代碼示例

    這是一個加密軟件,但只限于文本加密,加了窗口控件的滑動效果,詳細看下面的代碼
    2013-11-11
  • C#如何控制IIS動態(tài)添加刪除網(wǎng)站詳解

    C#如何控制IIS動態(tài)添加刪除網(wǎng)站詳解

    這篇文章主要給大家介紹了關(guān)于C#如何控制IIS動態(tài)添加刪除網(wǎng)站的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 簡介C#讀取XML的兩種方式

    簡介C#讀取XML的兩種方式

    在程序中訪問進而操作XML文件一般有兩種模型,分別是使用DOM(文檔對象模型)和流模型,使用DOM的好處在于它允許編輯和更新XML文檔,可以隨機訪問文檔中的數(shù)據(jù),可以使用XPath查詢
    2013-03-03
  • C#與SQL連接:GridView控件對數(shù)據(jù)庫的操作

    C#與SQL連接:GridView控件對數(shù)據(jù)庫的操作

    GridView控件操作方面的知識,需要的朋友可以參考一下
    2013-02-02
  • C#實現(xiàn)從windows剪貼板獲取內(nèi)容的方法

    C#實現(xiàn)從windows剪貼板獲取內(nèi)容的方法

    這篇文章主要介紹了C#實現(xiàn)從windows剪貼板獲取內(nèi)容的方法,涉及C#操作剪貼板的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下
    2015-05-05
  • 基于Avalonia實現(xiàn)自定義彈窗的示例詳解

    基于Avalonia實現(xiàn)自定義彈窗的示例詳解

    對于使用avalonia的時候某些功能需要到一些提示,比如異?;蛘叱晒Χ夹枰獙τ脩暨M行提示,所以需要單獨實現(xiàn)彈窗功能,并且可以自定義內(nèi)部組件,這一期將手動實現(xiàn)一個簡單的小彈窗,并且很容易自定義,希望大家喜歡
    2023-02-02
  • Unity3D基于陀螺儀實現(xiàn)VR相機功能

    Unity3D基于陀螺儀實現(xiàn)VR相機功能

    這篇文章主要為大家詳細介紹了Unity3D基于陀螺儀實現(xiàn)VR相機功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#實現(xiàn)會移動的文字效果

    C#實現(xiàn)會移動的文字效果

    這篇文章主要為大家詳細介紹了C#實現(xiàn)會移動的文字效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04

最新評論