C#中委托用法
本文實(shí)例講述了C#中委托用法。分享給大家供大家參考。具體分析如下:
對(duì)于用戶要查找的條件的千變?nèi)f化,我們?cè)趯?xiě)條件去查找時(shí),是不可能一下寫(xiě)死的,那樣,如果你寫(xiě)好了一個(gè)類讓別人用,別人需要的不是那種查詢,得去找你改條件.
那么我們能否讓使用這個(gè)類的人自己定義一個(gè)規(guī)則(條件),直接傳條件給你,你幫我查詢出結(jié)果來(lái),C#就可以用委托來(lái)解決,相應(yīng)的java可以用接口來(lái)實(shí)現(xiàn)
using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace FinderTest { //性別枚舉 public enum Genders { male=1,female=2 } //學(xué)生類 public class Student { public Student() { } public Student(int _id, string _name, Genders _gender, DateTime _birthday, string _telephone) { this._id = _id;//學(xué)生id this._name = _name;//學(xué)生姓名 this._gender = _gender;//學(xué)生性別 this._birthday = _birthday;//學(xué)生生日 this._telephone = _telephone;//學(xué)生電話 } int _id; public int Id { get { return _id; } set { _id = value; } } string _name; public string Name { get { return _name; } set { _name = value; } } Genders _gender; public Genders Gender { get { return _gender; } set { _gender = value; } } DateTime _birthday; public DateTime Birthday { get { return _birthday; } set { _birthday = value; } } private string _telephone; public string Telephone { get { return _telephone; } set { _telephone = value; } } public void show() { Console.WriteLine(string.Format("我的姓名:{0}/t學(xué)號(hào):{1}/t性別:{2}",_name,_id,_gender)); } } }
using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace FinderTest { //學(xué)期枚舉 public enum Semesters { x1 = 1, x2 = 2, x3 = 3 } public delegate bool Predicate(Student s);//定義一個(gè)委托 //班級(jí)類 public class Class : ArrayList { public Class() { } public Class(string _name, string _master, Semesters _semester) { this._name = _name; this._master = _master; this._semester = _semester; _allStudents = new ArrayList(); } private string _name;//班級(jí)名字 public string Name { get { return _name; } set { _name = value; } } private string _master;//班長(zhǎng) public string Master { get { return _master; } set { _master = value; } } private Semesters _semester;//學(xué)期 public Semesters Semester { get { return _semester; } set { _semester = value; } } //班級(jí)里的學(xué)生集合 ArrayList _allStudents; public ArrayList AllStudents { get { return _allStudents; } } public ArrayList FindAll(Predicate match) { if (match == null) { return this._allStudents; } ArrayList result = new ArrayList(); for (int i = 0; i < this._allStudents.Count; i++) { Student one = (Student)this._allStudents[i]; if (match(one)) { result.Add(one); } } return result; } } }
using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace FinderTest { class Program { static void Main(string[] args) { Class c1 = new Class("0603", "jsp", Semesters.x1); Student s1 = new Student(1, "zs", Genders.male, DateTime.Parse("1988-02-23"), "13088522635"); Student s2 = new Student(2, "ls", Genders.female, DateTime.Parse("1986-12-03"), "13188522888"); Student s3 = new Student(3, "ww", Genders.female, DateTime.Parse("1988-11-15"), "13288576885"); Student s4 = new Student(4, "zl", Genders.male, DateTime.Parse("1984-02-21"), "13388534635"); Student s5 = new Student(5, "qq", Genders.female, DateTime.Parse("1988-02-23"), "13488524335"); Student s6 = new Student(6, "cb", Genders.male, DateTime.Parse("1989-02-23"), "13588527636"); c1.AllStudents.Add(s1); c1.AllStudents.Add(s2); c1.AllStudents.Add(s3); c1.AllStudents.Add(s4); c1.AllStudents.Add(s5); c1.AllStudents.Add(s6); ArrayList list= c1.FindAll(match); //查找班級(jí)女生的資料 // ArrayList list = c1.FindAll(match1); //查找學(xué)號(hào)從1到5的學(xué)生 foreach (Student s in list) { s.show(); } } //條件為女性 public static bool match(Student s) { if (s.Gender.Equals(Genders.female)) { return true; } return false; } //條件為學(xué)號(hào)從1到5 public static bool match1(Student s) { if (s.Id.CompareTo(1) >= 0 && s.Id.CompareTo(5) <= 0) { return true; } return false; } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#基于委托實(shí)現(xiàn)多線程之間操作的方法
- C#中委托用法實(shí)例詳解
- C#用匿名方法定義委托的實(shí)現(xiàn)方法
- C#通過(guò)委托調(diào)用Button單擊事件的方法
- C#泛型委托的用法實(shí)例分析
- C#使用委托(delegate)實(shí)現(xiàn)在兩個(gè)form之間傳遞數(shù)據(jù)的方法
- C#異步委托調(diào)用實(shí)例分析
- C#中使用委托的3種方式代碼示例
- C#中異步回調(diào)函數(shù)用法實(shí)例
- 詳解C#中通過(guò)委托來(lái)實(shí)現(xiàn)回調(diào)函數(shù)功能的方法
相關(guān)文章
使用C#實(shí)現(xiàn)在屏幕上畫(huà)圖效果的代碼實(shí)例
本篇文章是對(duì)使用C#在屏幕上畫(huà)圖效果的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C#使用NAudio實(shí)現(xiàn)監(jiān)聽(tīng)系統(tǒng)聲音
這篇文章主要為大家詳細(xì)介紹了C#如何使用NAudio實(shí)現(xiàn)監(jiān)聽(tīng)系統(tǒng)聲音并屏蔽麥克風(fēng)其他聲音,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2024-02-02C#中Foreach循環(huán)遍歷的本質(zhì)與枚舉器詳解
這篇文章主要給大家介紹了關(guān)于C#中Foreach循環(huán)遍歷本質(zhì)與枚舉器的相關(guān)資料,foreach循環(huán)用于列舉出集合中所有的元素,foreach語(yǔ)句中的表達(dá)式由關(guān)鍵字in隔開(kāi)的兩個(gè)項(xiàng)組成,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08C#設(shè)計(jì)模式之裝飾器模式實(shí)例詳解
本文詳細(xì)講解了C#設(shè)計(jì)模式之裝飾器模式,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10c# 實(shí)現(xiàn)RSA非對(duì)稱加密算法
RSA解決了對(duì)稱加密的一個(gè)不足,比如AES算法加密和解密時(shí)使用的是同一個(gè)秘鑰,因此這個(gè)秘鑰不能公開(kāi),因此對(duì)于需要公開(kāi)秘鑰的場(chǎng)合,我們需要在加密和解密過(guò)程中使用不同的秘鑰,加密使用的公鑰可以公開(kāi),解密使用的私鑰要保密,這就是非對(duì)稱加密的好處?!?/div> 2021-06-06關(guān)于C#中ajax跨域訪問(wèn)問(wèn)題
最近做項(xiàng)目,需要跨域請(qǐng)求訪問(wèn)數(shù)據(jù)問(wèn)題。下面通過(guò)本文給大家分享C#中ajax跨域訪問(wèn)代碼詳解,需要的朋友可以參考下2017-05-05C#實(shí)現(xiàn)啟動(dòng)項(xiàng)管理的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)啟動(dòng)項(xiàng)管理,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下2022-12-12unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周
這篇文章主要介紹了unity 實(shí)現(xiàn)攝像機(jī)繞某點(diǎn)旋轉(zhuǎn)一周,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04協(xié)定需要會(huì)話,但是綁定“BasicHttpBinding”不支持它或者因配置不正確而無(wú)法支持它
在IIS7及以上版本服務(wù)器中提供了基于WAS的無(wú).SVC文件的WCF服務(wù)激活功能,能夠提供基于HTTP和非HTTP協(xié)議的訪問(wèn),通過(guò)添加Windows Server AppFabric可以更方便的管理WCF服務(wù)2012-12-12最新評(píng)論