C#中 this关键字 四种用法

/// <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的四种用法,做简单的总结。

 

posted @ 2015-06-04 15:23  DotNet码农  阅读(906)  评论(0编辑  收藏  举报
/*粒子线条,鼠标移动会以鼠标为中心吸附的特效*/