zoukankan      html  css  js  c++  java
  • C#中this的用法,你用过几种?

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

    摘自:http://www.2cto.com/kf/201110/107591.html

    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();

     

     

    四种用法完整代码如下:


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

     

     

    由于时间关系,就不说太多,如有不足之处,恳请大家批评指正。

    完整示例源码下载:http://up.2cto.com/2011/1013/20111013110305836.rar
    摘自:零星碎事 

  • 相关阅读:
    Spring Boot @DeleteMapping(转)
    白天和夜晚分类
    使用HSV色彩空间遮罩绿色区域
    HSV转换
    HSV 和 HLS颜色空间
    python 处理图像出现The lower bounary is neither an array of the same size and same type as src, nor a scalar in function inRange
    读取图片上一点的像素
    python 实现A*算法
    uda 4.C++面向对象编程
    uda 3.C++二维向量
  • 原文地址:https://www.cnblogs.com/nov5026/p/4661588.html
Copyright © 2011-2022 走看看