摘要:本文主要向大家介绍了C#编程之C#中this的 四种 用法,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
本文主要向大家介绍了C#编程之C#中this的 四种 用法,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。
C#中的this用法,相信大家应该有用过,但你用过几种?以下是个人总结的this几种用法,欢迎大家拍砖,废话少说,直接列出用法及相关代码。
this用法1:限定被相似的名称隐藏的成员
///
///
public Person(string Name, string Sex)
{
this.Name = Name;
this.Sex = Sex;
}
this用法2:将对象作为参数传递到其他方法
///
public class Person
{
///
public string Name { set; get; }
///
public void ShowName()
{
Helper.PrintName(this);
}
}
///
public static class Helper
{
///
///
public static void PrintName(Person person)
{
HttpContext.Current.Response.Write("姓名:" + person.Name + "
");
}
}
this用法3:声明索引器
///
public NameValueCollection Attr = new NameValueCollection();
///
///
///
public string this[string key]
{
set
{
Attr[key] = value;
}
get
{
return Attr[key];
}
}
this用法4:扩展对象的方法
///
public class Person
{ ///
public string Sex { set; get; }
}
///
public static class Helper
{
///
///
///
public static string GetSex(this Person item)
{
return item.Sex;
}
}
调用:
Person person = new Person();
person.GetSex();
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号