国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

c# 委托詳解

2019-11-17 03:10:25
字體:
來源:轉載
供稿:網友

c# 委托詳解

1.委托聲明

2.委托入門實例

namespace Consoleapplication1{        public delegate void methodDelegate(string str);    class PRogram    {        static void Main(string[] args)        {            Student student = new Student();            Teacher teacher = new Teacher("王老師");            methodDelegate methodDelegate1 = new methodDelegate(student.getStudentName);            methodDelegate1 += teacher.getTeacherName; //可以指向不同類中的方法!            //methodDelegate1 += teacher.getClassName; 指向簽名不符的方法時提示錯誤!            methodDelegate1.Invoke("張三");            Console.ReadLine();        }    }    class Student    {        private String name = "";        public Student (String _name)        {            this.name = _name ;        }        public Student() {}        public void getStudentName(String _name)         {            if (this.name != "" )                Console.WriteLine("Student's name is {0}", this.name);            else                Console.WriteLine("Student's name is {0}", _name);        }    }    class Teacher    {        private String name;        public Teacher(String _name)        {            this.name = _name;        }        public void getTeacherName(String _name)        {            if (this.name != "")                Console.WriteLine("Teacher's name is {0}", this.name);            else                Console.WriteLine("Teacher's name is {0}", _name);        }        public string getClassName()         {            return "Eanlish";        }    }}

上面代碼中實現對委托的調用

最后將被調用的委托輸出

3.委托的實現方式

第一種:常規實現

private delegate String getAString( string parament);        static void Main(String []args)        {            int temp = 40;            getAString stringMethod = new getAString(temp.ToString); //傳遞temp.ToString調用委托            Console.WriteLine("String is {0}", stringMethod());  //stringMethod()調用已經接受參數的委托            Console.ReadLine();        }

第二種:多播委托

 getAString stringMethod = new getAString(temp.ToString);         stringMethod += temp.ToString;         stringMethod -= temp.ToString;  

  這種調用之后,按照接受參數的次數, 委托會生成一個列表,每用+=調用一次,會增加一個列表項,-=調用一次,會移除一個列表項。

第三種:委托數組

當我們定義委托 讓委托形成一個數組的時候,我們可以通過遍歷數組的方式來調用它

delegate double Operations(double x);    class Program    {        static void Main()        {            Operations[] operations =            {               MathOperations.MultiplyByTwo,               MathOperations.Square            };            for (int i = 0; i < operations.Length; i++)            {                Console.WriteLine("Using operations[{0}]:", i);                DisplayNumber(operations[i], 2.0);                DisplayNumber(operations[i], 7.94);                Console.ReadLine();            }        }        static void DisplayNumber(Operations action, double value)        {            double result = action(value);            Console.WriteLine(               "Input Value is {0}, result of operation is {1}", value, result);        }    }    struct MathOperations    {        public static double MultiplyByTwo(double value)        {            return value * 2;        }        public static double Square(double value)        {            return value * value;        }    }

上面實例中

將委托定義好

之后就可以遍歷它

第四種:Action<T>和Func<T>委托

利用微軟預先定義的兩個泛型類型來給我們調用

先看語法知識

Action<T>

Func<T>

二者都可以接受最大參數個數為16個,二者的區別在于一個有返回值,另外一個返回值為void

實例

using System;namespace DelegateFuncAction{    class Program    {        static void Main(string[] args)        {            Func<double, double,double> DoAddtion = calculate.addtion;            double result = DoAddtion(20, 30);            Console.WriteLine("Func帶返回參數委托做加法結果為:{0}",DoAddtion(10,20));            calculate c=new calculate();            Action<double, double> DoSubstraction = c.substraction;            DoSubstraction(90, 20);        }    }    class calculate    {        public static double addtion(double x, double y)        {            return x + y;        }        public void substraction(double x, double y)        {            Console.WriteLine("Action不帶返回參數委托做減法結果為:{0}",x-y);        }    }}

二者定義的方法名都可以把函數賦予它 如下

第五種:匿名方法

匿名方法基本上和上面沒有區別,只是實例化時候不同而已

class Program    {        static void Main()        {            double width = 12;            Func<double, double> square = delegate(double length)              {                length *= width;                return length;            };            Console.WriteLine(square(10));        }    }

這樣減少了代碼的編寫 將一個函數直接在定義的時候賦予Func<T>或者Action<T>定義的函數

第六種:Lambda表達式

先看Lambda表達式的語法知識

例如

delegate int del(int i);static void Main(string[] args){    del myDelegate = x => x * x;    int j = myDelegate(5); //j = 25}

實例中

 x => x * x就是一個參數為x  執行x*x的lambda的表達式  賦予一個委托

4.總結

在聲明委托時候可以用三種方式來聲明

1.用 delegate關鍵詞來聲明(需要聲明返回值,接受參數)

2.Action<T>來聲明一個返回值為void類型的委托

3.Func<T>來聲明有返回類型的委托

委托的實現

無論委托是如何實現的,我們都是賦予它函數時候體現的


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柘荣县| 塘沽区| 常山县| 遂川县| 广宁县| 蒲江县| 岑溪市| 观塘区| 兴业县| 曲沃县| 资中县| 济南市| 米易县| 昂仁县| 台中县| 佛坪县| 紫阳县| 富锦市| 河北省| 泸水县| 聊城市| 广宗县| 大姚县| 渝北区| 泗洪县| 安岳县| 乌恰县| 五大连池市| 金门县| 大同市| 普宁市| 密云县| 翼城县| 林西县| 山东省| 敖汉旗| 永善县| 连平县| 敦化市| 阿鲁科尔沁旗| 林甸县|