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 />");



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值