#中的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();
四种用法完整代码如下:
show sourceusing 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 />");
}
}
调用示例:
show source//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 />");