this用法1:引用类的当前实例,限定被相似的名称隐藏的成员
如:
public class Person
{
public string Name{get;set;}
public string Age{get;set;}
public Person(string Name,string Age)
{
this.Name=Name;
this.Age=Age;
}
}
this用法2:将对象作为参数传递到其他方法
如:
public static class Helper
{
public static void PrintName(Person person)
{
MessageBox.Show("姓名:"+person.Name);
}
}
this用法3:扩展方法的对象
如:
public enum StateLevelEnum
{
/// <summary>
/// 非关键指标(灰色)
/// </summary>
NotKeyItem = 0,
/// <summary>
/// 正常(绿色)
/// </summary>
Normal = 1,
/// <summary>
/// 二级告警级别(黄色)
/// </summary>
Level2 = 2,
/// <summary>
/// 一级告警级别(红色)
/// </summary>
Level1 = 3
}
public static string ToUIString(this StateLevelEnum level)
{
string result = "";
switch (level)
{
case StateLevelEnum.NotKeyItem:
result = "非关键指标";
break;
case StateLevelEnum.Normal:
result = "正常";
break;
case StateLevelEnum.Level2:
result = "二级告警";
break;
case StateLevelEnum.Level1:
result = "一级告警";
break;
default:
break;
}
return result;
}
调用:StateLevelEnum.Normal.ToUIString(); 返回 "正常"