Csharp/C#教程:C#中委托用法分享

本文实例讲述了C#中委托用法。分享给大家供大家参考。具体分析如下:

对于用户要查找的条件的千变万化,我们在写条件去查找时,是不可能一下写死的,那样,如果你写好了一个类让别人用,别人需要的不是那种查询,得去找你改条件.

那么我们能否让使用这个类的人自己定义一个规则(条件),直接传条件给你,你帮我查询出结果来,C#就可以用委托来解决,相应的java可以用接口来实现

usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Collections; namespaceFinderTest { //性别枚举 publicenumGenders { male=1,female=2 } //学生类 publicclassStudent { publicStudent() {} publicStudent(int_id,string_name,Genders_gender,DateTime_birthday,string_telephone) { this._id=_id;//学生id this._name=_name;//学生姓名 this._gender=_gender;//学生性别 this._birthday=_birthday;//学生生日 this._telephone=_telephone;//学生电话 } int_id; publicintId { get{return_id;} set{_id=value;} } string_name; publicstringName { get{return_name;} set{_name=value;} } Genders_gender; publicGendersGender { get{return_gender;} set{_gender=value;} } DateTime_birthday; publicDateTimeBirthday { get{return_birthday;} set{_birthday=value;} } privatestring_telephone; publicstringTelephone { get{return_telephone;} set{_telephone=value;} } publicvoidshow() { Console.WriteLine(string.Format("我的姓名:{0}/t学号:{1}/t性别:{2}",_name,_id,_gender)); } } } usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Collections; namespaceFinderTest { //学期枚举 publicenumSemesters { x1=1,x2=2,x3=3 } publicdelegateboolPredicate(Students);//定义一个委托 //班级类 publicclassClass:ArrayList { publicClass() {} publicClass(string_name,string_master,Semesters_semester) { this._name=_name; this._master=_master; this._semester=_semester; _allStudents=newArrayList(); } privatestring_name;//班级免费精选名字大全 publicstringName { get{return_name;} set{_name=value;} } privatestring_master;//班长 publicstringMaster { get{return_master;} set{_master=value;} } privateSemesters_semester;//学期 publicSemestersSemester { get{return_semester;} set{_semester=value;} } //班级里的学生集合 ArrayList_allStudents; publicArrayListAllStudents { get{return_allStudents;} } publicArrayListFindAll(Predicatematch) { if(match==null) { returnthis._allStudents; } ArrayListresult=newArrayList(); for(inti=0;i<this._allStudents.Count;i++) { Studentone=(Student)this._allStudents[i]; if(match(one)) { result.Add(one); } } returnresult; } } }
usingSystem; usingSystem.Collections.Generic; usingSystem.Text; usingSystem.Collections; namespaceFinderTest { classProgram { staticvoidMain(string[]args) { Classc1=newClass("0603","jsp",Semesters.x1); Students1=newStudent(1,"zs",Genders.male,DateTime.Parse("1988-02-23"),"13088522635"); Students2=newStudent(2,"ls",Genders.female,DateTime.Parse("1986-12-03"),"13188522888"); Students3=newStudent(3,"ww",Genders.female,DateTime.Parse("1988-11-15"),"13288576885"); Students4=newStudent(4,"zl",Genders.male,DateTime.Parse("1984-02-21"),"13388534635"); Students5=newStudent(5,"qq",Genders.female,DateTime.Parse("1988-02-23"),"13488524335"); Students6=newStudent(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); ArrayListlist=c1.FindAll(match); //查找班级女生的资料 //ArrayListlist=c1.FindAll(match1); //查找学号从1到5的学生 foreach(Studentsinlist) { s.show(); } } //条件为女性 publicstaticboolmatch(Students) { if(s.Gender.Equals(Genders.female)) { returntrue; } returnfalse; } //条件为学号从1到5 publicstaticboolmatch1(Students) { if(s.Id.CompareTo(1)>=0&&s.Id.CompareTo(5)<=0) { returntrue; } returnfalse; } } }

希望本文所述对大家的C#程序设计有所帮助。

您可能感兴趣的文章:C#基于委托实现多线程之间操作的方法C#中委托用法实例详解C#用匿名方法定义委托的实现方法C#通过委托调用Button单击事件的方法C#泛型委托的用法实例分析C#使用委托(delegate)实现在两个form之间传递数据的方法C#异步委托调用实例分析C#中使用委托的3种方式代码示例C#中异步回调函数用法实例详解C#中通过委托来实现回调函数功能的方法

C++ 中placement new 操作符使用方法

数据结构之数组Array实例详解

上述就是C#学习教程:C#中委托用法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/906474.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐