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

C#中this的 四种 用法的更多相关文章

  1. javascript中this的四种用法

    javascript中this的四种用法 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-05-11我要评论 在javascript当中每一个function都是一个对象,所 ...

  2. C#中 this关键字 四种用法

    /// <summary> /// 主程序入口 /// </summary> /// <param name="args"></param ...

  3. c++中for的四种用法

    #include <algorithm> #include <vector> #include <iostream> using namespace std; in ...

  4. 【转】javascript中this的四种用法

    在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this 出现的场景 ...

  5. js中this 的四种用法

    this 在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this  ...

  6. VBA中Option的四种用法

    1.Option Explicit.当使用Option Explicit时,必须在模块中的所有过程声明每一个变量,否则会出现语法错误并不能被编译.这样做的好处是,它能消除程序中因为错拼变量名而导致程序 ...

  7. JS中this的四种用法

    1.在一般函数方法中使用 this 指代全局对象 2.作为对象方法调用,this 指代上级对象 3.作为构造函数调用,this 指代new 出的对象 4.apply 调用 ,apply方法作用是改变函 ...

  8. JS中 this 的四种用法

    1.在一般函数中使用 this 指全局对象 window function fn(){ this.x = 1 } fn(); //相当于window.fn() 2.作为对象方法使用 this 指该对象 ...

  9. mysql中模糊查询的四种用法介绍

    下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...

随机推荐

  1. nodejs express 框架解密5-视图

    本文档是基于express 3.4.6 的 在我们的代码中,渲染模板大致是这样写的 exports.index = function(req, res){ res.render('index', { ...

  2. nodejs express 框架解密2-如何创建一个app

    本文是基于express 3.4.6 的 1.在我们的app.js 文件里面有这么几行 http.createServer(app).listen(app.get('port'), function( ...

  3. 如何清除朗逸保养提示标志INSP

    自己消除insp小扳手方法 具体步骤如下1.插入钥匙,不要转动.2.按住显示屏下方右边的黑圆柱按钮 3.钥匙转到2档,通电自检,期间按住按钮不要松手4.过10秒左右,INSP消失,小扳手自己去除 不用 ...

  4. Linux查看端口、进程情况及kill进程

    看端口: ps -aux | grep tomcat 发现并没有8080端口的Tomcat进程. 使用命令:netstat –apn 查看所有的进程和端口使用情况.发现下面的进程列表,其中最后一栏是P ...

  5. adding validation annotators to model classes 在linq to EntityFrame的Model中添加前台验证validation annotators

    The same solution can be applied for LINQ to SQL. The snippet the article shows for using the Metada ...

  6. AppStore 相关

    App 跳转 AppStore 网址链接   https://itunes.apple.com/app/uri/id582319843?mt=8   https 可替换成 itms,可直接避免进入 S ...

  7. MTNET 自用ios网络库开源

    短短两天就在https://git.oschina.net/gangwang/MTNET这里收获15个星 github 5星, 值得收藏! MTNET 自用ios网络库开源, 自用很久了,在数歀上架的 ...

  8. Embedding Python in C

    http://codextechnicanum.blogspot.com/2013/12/embedding-python-in-c-converting-c.html //Make some vec ...

  9. 安卓开发笔记——深入Activity

    在上一篇文章<安卓开发笔记——重识Activity >中,我们了解了Activity生命周期的执行顺序和一些基本的数据保存操作,但如果只知道这些是对于我们的开发需求来说是远远不够的,今天我 ...

  10. java中静态代码块的用法 static用法详解(转)

    (一)java 静态代码块 静态方法区别一般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情况下,其他程序 ...