C# this 关键字

一、代表当前实例对象

1      #region 代表当前实例对象
2         public string Name = "全局变量";
3         public void GetResult()
4         {
5             this.Name = "局部变量";
6             Console.WriteLine(this.Name);
7         }
8         #endregion

二、用this串联构造函数

 1     #region  用this串联构造函数
 2         public ThisKey()
 3         {
 4             Console.WriteLine("无参数构造函数");
 5         }
 6 
 7         public ThisKey(string name) : this()
 8         {
 9             Console.WriteLine(name);
10             Console.WriteLine("有参数构造函数");
11         }
12 
13         public ThisKey(string text, int number) : this(text)
14         {
15 
16         }
17     #endregion

三、扩展方法

 1       #region 扩展方法
 2         public int Count<T>(this IEnumerable<T> Source)
 3         {
 4             return Source.Count();
 5         }
 6 
 7         #endregion
 8 
 9             var sources = new List<People>
10             {
11                  new People()
12                  {
13                       ID=1,
14                       Name="2"
15                  },
16                  new People()
17                  {
18                       ID=1,
19                       Name="2"
20                  }
21             };
22             var count = thisKey.Count<People>(sources);
23             Console.WriteLine(count);

猜你喜欢

转载自www.cnblogs.com/GreatPerson/p/11099214.html