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] ...
随机推荐
- IOS开发 应用程序图标数字角标
其实实现这个功能很简单,只要调用UIApplication即可. 用法用例:[UIApplication sharedApplication].applicationIconBadgeNumber ...
- PropertyPlaceholderConfigurer的用法:
用法1: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...
- fio terse输出详解
fio, the flexible IO tester, is a very useful tool for benchmarking IO performance. It has an option ...
- Gradle自定义你的BuildConfig
BuildConfig.DEBUG 首先在Gradle脚本中默认的debug和release两种模式BuildCondig.DEBUG字段分别为true和false,而且不可更改.该字段编译后自动生成 ...
- 【News】SpagoBI中国官方微信对外发布
爱数据,爱分析,爱达比 致力于国际优秀开源BI应用在中国的普及推广,用数据开启中小企业智慧之门: 扫描上图二维码,关注SpagoBI中国官方微信,2015年8月18日将在腾讯课堂.百度传课.网易云课堂 ...
- ecshop 进入后台登录帐号出现乱码解决方法
进入后台登录帐号出现如果生成乱码 MySQL server error report:Array ( [0] => Array ( [message] => MySQL Query Err ...
- [原]quick集成spine动画
更新说明: 新增了骨骼绑定node用法 参考:http://blog.csdn.net/n5/article/details/21795265 在SkeletonRenderer.h 和cpp里面新加 ...
- 11款样式新颖的 jQuery/CSS3 网页菜单
今天为大家准备了11款样式风格挺不错的jQuery/CSS3网页菜单,主要包括面包屑菜单.下拉菜单.Tab菜单等,喜欢的朋友赶紧收藏,一起来看看这些菜单. 1.jQuery / CSS3多功能下拉菜单 ...
- windows 程序设计自学:添加图标资源
#include <windows.h> #include "resource.h" LRESULT CALLBACK MyWndProc( HWND hwnd, // ...
- 理解RxJava:(三)RxJava的优点
理解RxJava:(三)RxJava的优点 在第一部分,讲解了RxJava的基本结构.在第二部分,展示了operators的强大之处.但是你们可能仍然没有被说服,也没有足够的理由信服.下面是一些能让你 ...