您的位置:首页 > 编程语言 > C#

C#中 this关键字 四种用法

2015-06-04 15:23 465 查看
/// <summary>
/// 主程序入口
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{

//0>声明实体
User user = new User();
user.ID = 1;
user.UserName = "lichaoqiang";

//第【一】种用法:this用作索引器 public object this[string name]{……}
user["UserID"] = 1;
Console.WriteLine("第【一】种用法:this用作索引器");

//第【二】种用法:this用作参数传递 user.Say(this);
Console.WriteLine("第【二】种用法:this用作参数传递");
user.Said();

//第【三】种用法:this() public VIP:this(){   }
VIP vip = new VIP("yezi");
vip.Said();
Console.WriteLine("第【三】种用法:this()");

//第【四】种用法: this扩展VIP类 public static Sing(this User user){……}
Console.WriteLine("第【四】种用法: this扩展VIP类");
user.Sing();

Console.Read();

}


C# 中 this 关键字引用类的当前实例,还可用作扩展方法的第一个参数的修饰符。下面就针对this的四种用法,做简单的总结。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: