C#-扩展方法,运算符重载,匿名方法,内置委托.LINQ语法
1.扩展方法
为引用的类型追加方法,可提高代码编写效率,增加代码可读性;
例如有一个字符串,要对其进行2个操作:转换为整数;按1分割为数组;
1 2 3 | string str = "0121314" ; //给出字符串 int iValue = Convert.ToInt32(str); //转换为整数 string [] arrStr = str.Split( '1' ); //按1分隔为数组 |
我觉得上述写法不整洁,使用扩展方法对string类型追加几个方法,使其能实现上述2个功能:
1 2 3 4 5 6 7 8 9 10 11 | public static class StringExpand //类型命名要规范,必须是静态类 { public static int ToInt( this string str) //必须是静态函数,首个参数必须写为this string xxx; { return Convert.ToInt32(str); } public static string [] ToArray( this string str, char c) //可指定任意数量参数 { return str.Split(c); } } |
现在我可以如下实现功能:
1 2 3 | string str = "0121314" ; //给出字符串 int iValue = str.ToInt(); //转换为整数 string [] arrStr = str.ToArray( '1' ); //按1分隔为数组 |
附带个字符串转枚举的写法:
1 2 3 4 | public static T ToEnum<T>( this string str) { return (T)Enum.Parse( typeof (T), str); } |
以上为扩展方法编写与使用,例子比较简单,实际应用中可将频繁/繁琐的操作或逻辑判断进行封装,使代码编写更友好。
2.运算符重载
比如+-*/!等运算符常用于数值的计算,但事件的注册与解绑不是计算也可用+=/-=这种写法,这样看起来代码简洁可读性高
举例:有一个学习小组,有若干学生,现在要实现组内学生的加入或离开,常用写法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class StudyGroup { List<Student> stus = new List<Student>(); public void Add(Student stu) { stus.Add(stu); } public void Remove(Student stu) { stus.Remove(stu); } } ******************************************* StudyGroup grp = new StudyGroup(); Student stu = new Student(); grp.Add(stu); //加入 grp.Remove(stu); //离开 |
对运算符进行重载后,可使用如下写法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class StudyGroup { List<Student> stus = new List<Student>(); public static StudyGroup operator +(StudyGroup grp, Student stu) // { grp.stus.Add(stu); return grp; } public static StudyGroup operator -(StudyGroup grp, Student stu) { grp.stus.Remove(stu); return grp; } } ********************************************* StudyGroup grp = new StudyGroup(); Student stu = new Student(); grp += stu; //加入 grp -= stu; //离开 |
3.匿名方法
常用于事件注册,比如我要给一个按钮事件注册一个方法,我又不想写一个函数,可以如下写法:
1 2 3 4 5 | this .button2.Click += button2_Click; //绑定一个函数 this .button2.Click += ( object sender, EventArgs e) => //绑定一个匿名函数 { //事件处理 }; |
4.内置委托
.net自带的委托Action/Func/Predicate,可满足大部分需求,不用自己再去写了,给出方法如下:
1 2 3 4 5 6 | public void Test() { } public void Test1( int i) { } public void Test2( int i, string s) { } public string Test3() { return "" ; } public string Test4( int i) { return "" ; } public bool Test5( int i) { return false ; } |
使用内置委托:
1 2 3 4 5 6 7 | Action action = Test; Action< int > action1 = Test1; Action< int , string > action2 = Test2; //Action支持任意数量类型参数,但不具有返回值 Func< string > action3 = Test3; Func< int , string > action4 = Test4; Func< int , bool > actino5 = Test5; //Func支持任意类型数量参数,有一个返回值。最后一个为返回值,前面的为参数 Predicate< int > action6 = Test5; //Predicate支持一个参数,有一个布尔类型的返回值 |
5.LINQ语法
1 2 | List< string > list = new List< string >(); var temp = from l in list where l.Length == 2 select l.Substring(1); //从list中找出长度为2的字符串并截取他们,然后返回一个可枚举类型IEnumerable<T>。 |
其中Where为筛选条件,可设置多重筛选;Select为要返回的元素类型,可按实际需求编写。
分类:
日常随笔
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)