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();
C#中this的 四种 用法的更多相关文章
- javascript中this的四种用法
javascript中this的四种用法 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-05-11我要评论 在javascript当中每一个function都是一个对象,所 ...
- C#中 this关键字 四种用法
/// <summary> /// 主程序入口 /// </summary> /// <param name="args"></param ...
- c++中for的四种用法
#include <algorithm> #include <vector> #include <iostream> using namespace std; in ...
- 【转】javascript中this的四种用法
在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this 出现的场景 ...
- js中this 的四种用法
this 在函数执行时,this 总是指向调用该函数的对象.要判断 this 的指向,其实就是判断 this 所在的函数属于谁. 在<javaScript语言精粹>这本书中,把 this ...
- VBA中Option的四种用法
1.Option Explicit.当使用Option Explicit时,必须在模块中的所有过程声明每一个变量,否则会出现语法错误并不能被编译.这样做的好处是,它能消除程序中因为错拼变量名而导致程序 ...
- JS中this的四种用法
1.在一般函数方法中使用 this 指代全局对象 2.作为对象方法调用,this 指代上级对象 3.作为构造函数调用,this 指代new 出的对象 4.apply 调用 ,apply方法作用是改变函 ...
- JS中 this 的四种用法
1.在一般函数中使用 this 指全局对象 window function fn(){ this.x = 1 } fn(); //相当于window.fn() 2.作为对象方法使用 this 指该对象 ...
- mysql中模糊查询的四种用法介绍
下面介绍mysql中模糊查询的四种用法: 1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示. 比如 SELECT * FROM [user] ...
随机推荐
- SSTable and Log Structured Storage: LevelDB
If Protocol Buffers is the lingua franca of individual data record at Google, then the Sorted String ...
- bash的循环中无法保存变量
在bash中,如果循环在一个子shell中运行,那么在循环中对循环外面的变量的更改将在循环退出后不可见.像下面的例子: #!/bin/sh python run.py | while read lin ...
- Akismet API 密钥(key)免费获取方法
Akismet插件是用户使用最广泛的垃圾评论插件,也是wordpress的创始人制作的,同时它也毫无疑问的成为wordpress的默认安装插件,这样的插件可以帮助用户解决垃圾评论的烦恼,而且也不用访客 ...
- node-webkit 笔记
NW.js is an app runtime based on Chromium and node.js. You can write native apps in HTML and JavaScr ...
- 通过PowerShell获取TCP响应(类Telnet)
通常情况下,为了检测指定的TCP端口是否存活,我们都是通过telnet指定的端口看是否有响应来确定,然而默认情况下win8以后的系统默认是不安装telnet的.设想一下如果你黑进了一个服务器,上面没装 ...
- 8个经典HTML5 3D动画赏析
HTML5技术已经越来越被我们所接受,特别是一些3D的动画特效.本文介绍的8个HTML5 3D动画并没有特别华丽的界面,但是比较实用,涉及到3D图片.3D图表.3D按钮等方面,一起来看看. 1.HTM ...
- [转载]JavaScript 中小数和大整数的精度丢失
标题: JavaScript 中小数和大整数的精度丢失作者: Demon链接: http://demon.tw/copy-paste/javascript-precision.html版权: 本博客的 ...
- 康力优蓝机器人 -- 优友U05类人型机器人发布
[寒武计划]优友U05类人型机器人发布: http://digi.tech.qq.com/a/20151124/043234.htm?pgv_ref=aio2015&ptlang=2052 北 ...
- IPFS搭建分布式文件系统 - 访问控制
IPFS 一个内容可寻址.对等的超媒体分发协议. IPFS网络中的节点形成分布式文件系统. 为什么要用IPFS? “IPFS and the Blockchain are a perfect matc ...
- 配置jenkins,并把iOS包自动上传至fir.im
安装jenkins,有两种方式 1.首先要安装 homebrew,利用homebrew来管理安装包十分方便,一条命令就可以 安装 homebrew命令 $ ruby -e "$(curl - ...