C#中this的用法,你用过几种?【含源码示例】

来源:互联网 发布:淘宝店铺装修代码下载 编辑:程序博客网 时间:2024/05/17 23:56

app_code:

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

}

 

 

default.aspx:

 

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        /*********************************
         *
         * 作者:Foolin
         * 邮箱:Foolin@126.com
         * 博客:http://www.cnblogs.com/foolin
         * 日期:2011-10-11 01:28:15
         *
         * *********************************/


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

    }
}

 

原创粉丝点击