博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中this的 四种 用法
阅读量:4553 次
发布时间:2019-06-08

本文共 1858 字,大约阅读时间需要 6 分钟。

C#中的this用法,相信大家应该有用过,但你用过几种?以下是个人总结的this几种用法,欢迎大家拍砖,废话少说,直接列出用法及相关代码。

 

this用法1:限定被相似的名称隐藏的成员

///      /// /******************************************/     /// /*  this用法1:限定被相似的名称隐藏的成员 */     /// /******************************************/     ///      ///      public Person(string Name, string Sex)   {         this.Name = Name;         this.Sex = Sex;   }

 

this用法2:将对象作为参数传递到其他方法

 

///  ///Person 的摘要说明 ///  public class Person {     ///      /// 姓名     ///      public string Name { set; get; }       ///      /// /*******************************************/     /// /* this用法2:将对象作为参数传递到其他方法 */     /// /*******************************************/     ///      public void ShowName()     {         Helper.PrintName(this);     }           }   ///  /// 辅助类 ///  public static class Helper {       ///      /// 打印人名     ///      ///      public static void PrintName(Person person)     {         HttpContext.Current.Response.Write("姓名:" + person.Name + "
"); } }

 

 

this用法3:声明索引器

 

///   /// 其它属性  ///   public NameValueCollection Attr = new NameValueCollection();  ///   /// /*************************/  /// /* this用法3:声明索引器 */  /// /*************************/  ///   ///   /// 
public string this[string key] { set { Attr[key] = value; } get { return Attr[key]; } }

 

this用法4:扩展对象的方法

 

///  ///Person 的摘要说明 ///  public class Person {   ///          /// 性别         ///          public string Sex { set; get; } }     ///  /// 辅助类 ///  public static class Helper {       ///      /// /*****************************/     /// /* this用法4:扩展对象的方法 */     /// /*****************************/     ///      ///      /// 
public static string GetSex(this Person item) { return item.Sex; } }调用:Person person = new Person(); person.GetSex();

 

转载于:https://www.cnblogs.com/x-poior/p/5665039.html

你可能感兴趣的文章
垂直居中的几种实现方法
查看>>
UILabel标签文字过长时的显示方式
查看>>
H5离线缓存机制-manifest
查看>>
比较:I/O成员函数getline() 与 get()(第二种用法)的用法异同
查看>>
201671010118 2016-2017-2《Java程序设计》 第十一周学习心得
查看>>
Get Sauce(状压DP)
查看>>
Office2007 升级到 office2010
查看>>
SpringBoot整合Hibernate
查看>>
PPT1 例2
查看>>
extern外部方法使用C#简单例子
查看>>
血液循环结构
查看>>
SQL Server统计数据库中表个数、视图个数、存储过程个数
查看>>
设计模式:观察者模式
查看>>
课程总结
查看>>
openstack新建虚机、网络、路由时候对应的ovs网桥的变化
查看>>
linux 编译运行c文件
查看>>
Scrapy的学习和使用
查看>>
7.内部类(一)之详解内部类
查看>>
1.messager消息提示框
查看>>
C teaching
查看>>