C#中this的用法,你用过几种?【含源码示例】
C#中的this用法,相信大家应该有用过,但你用过几种?以下是个人总结的this几种用法,欢迎大家拍砖,废话少说,直接列出用法及相关代码。
- this用法1:限定被相似的名称隐藏的成员
/// <summary> /// /******************************************/ /// /* this用法1:限定被相似的名称隐藏的成员 */ /// /******************************************/ /// </summary> /// <param name="Name"></param> public Person( string Name, string Sex) { this .Name = Name; this .Sex = Sex; } |
- this用法2:将对象作为参数传递到其他方法
/// <summary> ///Person 的摘要说明 /// </summary> public class Person { /// <summary> /// 姓名 /// </summary> public string Name { set ; get ; } /// <summary> /// /*******************************************/ /// /* this用法2:将对象作为参数传递到其他方法 */ /// /*******************************************/ /// </summary> public void ShowName() { Helper.PrintName( this ); } } /// <summary> /// 辅助类 /// </summary> public static class Helper { /// <summary> /// 打印人名 /// </summary> /// <param name="person"></param> public static void PrintName(Person person) { HttpContext.Current.Response.Write( "姓名:" + person.Name + "<br />" ); } } |
- this用法3:声明索引器
/// <summary> /// 其它属性 /// </summary> public NameValueCollection Attr = new NameValueCollection(); /// <summary> /// /*************************/ /// /* this用法3:声明索引器 */ /// /*************************/ /// </summary> /// <param name="key"></param> /// <returns></returns> public string this [ string key] { set { Attr[key] = value; } get { return Attr[key]; } } |
- this用法4:扩展对象的方法
/// <summary> ///Person 的摘要说明 /// </summary> public class Person { /// <summary> /// 性别 /// </summary> public string Sex { set ; get ; } } /// <summary> /// 辅助类 /// </summary> public static class Helper { /// <summary> /// /*****************************/ /// /* this用法4:扩展对象的方法 */ /// /*****************************/ /// </summary> /// <param name="item"></param> /// <returns></returns> public static string GetSex( this Person item) { return item.Sex; } } |
调用:
Person person = new Person(); person.GetSex(); |
四种用法完整代码如下:
using System; using System.Collections.Generic; using System.Web; using System.Collections; using System.Collections.Specialized; /// <summary> ///Person 的摘要说明 /// </summary> public class Person { /// <summary> /// 姓名 /// </summary> public string Name { set ; get ; } /// <summary> /// 性别 /// </summary> public string Sex { set ; get ; } /// <summary> /// 其它属性 /// </summary> public NameValueCollection Attr = new NameValueCollection(); public Person() { } /// <summary> /// /******************************************/ /// /* this用法1:限定被相似的名称隐藏的成员 */ /// /******************************************/ /// </summary> /// <param name="Name"></param> public Person( string Name, string Sex) { this .Name = Name; this .Sex = Sex; } /// <summary> /// /*******************************************/ /// /* this用法2:将对象作为参数传递到其他方法 */ /// /*******************************************/ /// </summary> public void ShowName() { Helper.PrintName( this ); } /// <summary> /// /*************************/ /// /* this用法3:声明索引器 */ /// /*************************/ /// </summary> /// <param name="key"></param> /// <returns></returns> public string this [ string key] { set { Attr[key] = value; } get { return Attr[key]; } } } /// <summary> /// 辅助类 /// </summary> public static class Helper { /// <summary> /// /*****************************/ /// /* this用法4:扩展对象的方法 */ /// /*****************************/ /// </summary> /// <param name="item"></param> /// <returns></returns> public static string GetSex( this Person item) { return item.Sex; } /// <summary> /// 打印人名 /// </summary> /// <param name="person"></param> public static void PrintName(Person person) { HttpContext.Current.Response.Write( "姓名:" + person.Name + "<br />" ); } } |
调用示例:
//this用法1示例 Person person = new Person( "小她" , "女" ); //this用法2示例 person.ShowName(); //this用法3示例 person[ "Height" ] = "175cm" ; Response.Write( "身高:" + person[ "Height" ] + "<br />" ); person[ "Weight" ] = "110kg" ; Response.Write( "体重:" + person[ "Weight" ] + "<br />" ); //this用法4示例 Response.Write( "性别:" + person.GetSex() + "<br />" ); |
由于时间关系,就不说太多,如有不足之处,恳请大家批评指正。
完整示例源码下载:https://files.cnblogs.com/foolin/UseThis.rar
作者:刘付灵(Foolin)
出处:http://www.cnblogs.com/foolin/
关于作者:专注于.Net、Windows Phone 7和移动互联网开发。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过Foolin(AT)126.com 联系我,非常感谢。
广而告知:http://www.liufuling.cn
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库