C#中this的用法集锦
程序员文章站
2023-10-26 23:39:04
this 关键字引用类的当前实例,还可用作扩展方法的第一个参数的修饰符。下面就针对this的四种用法,做简单的总结。
首先,我们分别创建user和vip两个c#类...
this 关键字引用类的当前实例,还可用作扩展方法的第一个参数的修饰符。下面就针对this的四种用法,做简单的总结。
首先,我们分别创建user和vip两个c#类
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace appconsole { /// <summary> /// 普通用户 /// </summary> public class user { /// <summary> /// 全局变量 /// </summary> dictionary<string, object> dictinfo = null; /// <summary> /// 构造器 /// </summary> public user() { dictinfo = new dictionary<string, object>(); } /// <summary> /// 构造函数重载 /// </summary> /// <param name="userid"></param> /// <param name="username"></param> public user(int userid, string username) { this.username = username; this.id = userid; } /// <summary> /// this,第【1】种用法,索引器 /// </summary> /// <param name="name"></param> /// <returns></returns> public object this[string name] { get { return dictinfo[name]; } set { dictinfo[name] = value; } } /// <summary> /// 编号 /// </summary> public int id { get; set; } /// <summary> /// 用户名 /// </summary> public string username { get; set; } /// <summary> /// this第【2】种用法,当做参数传递 /// </summary> public void said() { new vip().say(this); } } /// <summary> /// 会员 /// </summary> public class vip : user { /// <summary> /// 积分 /// </summary> public int integral { get; set; } /// <summary> /// 构造函数 /// </summary> public vip() { id = 520; integral = 1000; } /// <summary> /// this第【3】种用法,通过this()调用无参构造函数 /// </summary> /// <param name="username"></param> public vip(string username) : this() { this.username = username; } /// <summary> /// 构造函数重载 /// </summary> /// <param name="userid"></param> /// <param name="username"></param> public vip(int userid, string username) : base(userid, username) { } /// <summary> ///say方法 /// </summary> /// <param name="user"></param> public void say([lcqattribute] user user) { console.writeline(string.format("嗨,大家好!我的编号是{0},大家可以叫我{1}!", user.id, user.username)); } } /// <summary> /// 静态类,来扩展user类 /// </summary> public static class helper { /// <summary> /// 第【4】种用法: this扩展user类 /// </summary> /// <param name="user"></param> public static void sing(this user user) { console.writeline(string.format("嗨,大家好!我的编号是{0},大家可以叫我{1}!", user.id, user.username)); } } /// <summary> /// 特性类:指定特性仅适用于方法和方法的参数 /// </summary> [system.attributeusage(attributetargets.method | attributetargets.parameter)] public class lcqattribute : system.attribute { } }
this第【1】种用法,索引器
/// <summary> /// 全局变量 /// </summary> dictionary<string, object> dictinfo = null; /// <summary> /// this,第【1】种用法,索引器 /// </summary> /// <param name="name"></param> /// <returns></returns> public object this[string name] { get { return dictinfo[name]; } set { dictinfo[name] = value; } }
this第【2】种用法,当做参数传递
/// <summary> /// this第【2】种用法,当做参数传递 /// </summary> public void said() { new vip().say(this); }
this第【3】种用法,通过this()调用无参构造函数
/// <summary> /// this第【3】种用法,通过this()调用无参构造函数 /// </summary> /// <param name="username"></param> public vip(string username) : this() { this.username = username; }
this第【4】种用法:扩展user类
/// <summary> /// 静态类,来扩展user类 /// </summary> public static class helper { /// <summary> /// 第【4】种用法: this扩展user类 /// </summary> /// <param name="user"></param> public static void sing(this user user) { console.writeline(string.format("嗨,大家好!我的编号是{0},大家可以叫我{1}!", user.id, user.username)); } }
最后,控制台测试
/// <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(); }
最终结果示意图
以上所述上就是本文的全部内容了,希望大家能够喜欢。