C# 关键字:this

1、代表当前类的对象

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Teacher t = new Teacher();
 6             Console.WriteLine(t.getResult());
 7 
 8 
 9             Console.ReadKey();
10         }
11     }
12     public class Teacher
13     {
14         private string scope = "全局变量";
15         public string getResult()
16         {
17             string scope = "局部变量";
18             // this代表Teacher类的对象
19             // 所以this.scope对应的是全局变量
20             // scope对应的是getResult方法内的局部变量
21             return this.scope + "-" + scope;
22         } 
23     } 

运行结果:

2、显式的调用自己的构造函数

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Teacher t = new Teacher("张三");  
 6 
 7             Console.ReadKey();
 8         }
 9     }
10     public class Teacher
11     {
12         public Teacher()
13         {
14             Console.WriteLine("无参构造函数");
15         }
16         // this()对应无参构造方法Teacher()
17         // 先执行Teacher(),后执行Teacher(string text)
18         public Teacher(string text) : this()
19         {
20             Console.WriteLine(text);
21             Console.WriteLine("有参构造函数");
22         } 
23     } 

运行结果:

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Teacher t2 = new Teacher("张三", 18, '男');
 6             t2.SayHi();
 7 
 8             Console.ReadKey();
 9         }
10     }
11       
12     class Teacher
13     {
14         public string Name { get; set; }
15         public int Age { get; set; }
16         public char Gender { get; set; }
17 
18         public int Chinese { get; set; }
19         public int Math { get; set; }
20         public int English { get; set; }
21 
22 
23         public Teacher(string name, int age, char gender, int chinese, int math, int english)
24         {
25             this.Name = name;
26             this.Age = age;
27             this.Gender = gender;
28             this.Chinese = chinese;
29             this.Math = math;
30             this.English = english;
31         } 
32 
33         public Teacher(string name, int age, char gender)  : this(name, age, gender, 0, 0, 0)
34         { } 
35 
36         public void SayHi()
37         {
38             Console.WriteLine("我叫{0},今年{1}岁了,我是{2}生", this.Name, this.Age, this.Gender);
39         } 
40     }

运行结果:

3、为原始类型扩展方法

   static class Extends
    {
        public static object ToJson(this string Json)
        {
            return Json == null ? null : JsonConvert.DeserializeObject(Json);
        }
    }

4、索引器

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             int[] nums = { 1, 2, 3, 4, 5 };
 6             Console.WriteLine(nums[2]);
 7             Person p = new Person();
 8             p.Numbers = new int[] { 1, 2, 3, 4, 5 };
 9 
10             p[0] = 1;
11             p[1] = 2;
12             p[2] = 3;
13             p[3] = 4;
14             Console.WriteLine(p[0]);
15             Console.WriteLine(p[1]);
16 
17             p["张三"] = "嘿嘿嘿";
18             p["李四"] = "呵呵呵";
19             p["王五"] = "哈哈哈";
20             Console.WriteLine(p[3]);
21 
22             Console.ReadKey();
23         }
24     }
25 
26     class Person
27     {
28         public int[] Numbers { get; set; } = new int[5];
29 
30         //索引器 让对象以索引的方式操作数组
31         public int this[int index]
32         {
33             get { return Numbers[index]; }
34             set { Numbers[index] = value; }
35         } 
36 
37         Dictionary<string, string> dic = new Dictionary<string, string>();
38         public string this[string index]
39         {
40             get { return dic[index]; }
41             set { dic[index] = value; }
42         }
43     }

猜你喜欢

转载自www.cnblogs.com/zhaoyl9/p/13196225.html