当前位置:   article > 正文

C# this的五种用法

c# this

目录

一、需求

二、区分当前类的对象

三、作为参数传递

四、作为索引器

五、调用其他的构造函数

六、扩展静态类方法

结束


一、需求

一般来说,this 仅仅局限于对象内部,对象外部是无法看到的,这就是this的基本思想,在我们的项目开发中,this关键字用的并不多,这也导致有些程序员对它的认识就不充足,有些知识点就会错过,this的功能绝对不是网上一些课程讲的那样,只是用来区分全局变量和局部变量,下面我就介绍 this 的几种用法,必定能让你对这个关键字有一个全新的认识。

二、区分当前类的对象

这个是常用的功能,如下图,熟悉的可以直接跳过,假设当前类有一个全局变量和当前方法中的参数名一模一样的时候,Visual Studio 就会提示异常,因为系统不知道你到底要给谁赋值,按 C# 的编程规范来说,全局变量最好第一个字母用大写,当然你也可以用小写,在遇到下面的这种情况时,只要前面加一个 this,系统就知道左边的 name 是当前类的成员,而右边的 name 则是方法的参数。

这时的 this 我们看到是一个正常的蓝色,如果不需要用到 this 关键字,那么 this 字体则是灰色的。

三、作为参数传递

如果其他类的参数类型和当前类一致,直接写 this 即可

  1. public class Test1
  2. {
  3. public void MyTest(Test2 test2)
  4. {
  5. Console.WriteLine(test2.Name);
  6. }
  7. }
  8. public class Test2
  9. {
  10. public string Name = "厚礼蟹";
  11. public void MyTest()
  12. {
  13. new Test1().MyTest(this);
  14. }
  15. }

调用 Test2.MyTest() 就会输出:厚礼蟹

四、作为索引器

作为索引器,在平时的项目中用的并不多,但在微软给我们封装好的方法中,用的特别多,我们常用的数组,List 等,基本都是使用索引器去读取的。

  1. namespace 计算3
  2. {
  3. public class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. Test1 test1 = new Test1();
  8. Console.WriteLine(test1[1]);
  9. Console.ReadKey();
  10. }
  11. }
  12. public class Test1
  13. {
  14. string[] NameList = new string[3] { "张三", "李四", "王五" };
  15. public string this[int index]
  16. {
  17. get
  18. {
  19. if(index < 0 || index >= NameList.Length)
  20. {
  21. Console.WriteLine("index 的值超过了数组的范围");
  22. return null;
  23. }
  24. return NameList[index];
  25. }
  26. }
  27. }
  28. }

执行后,会输出:李四

五、调用其他的构造函数

在实例化当前的类的时候,不仅仅是调用一个构造函数,用 this 就可以调用其他的构造函数,甚至在调用的时候,还可以执行其他的属性,字段,调用其他的方法,这些都是没问题的。

  1. namespace 计算3
  2. {
  3. public class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. Test1 test1 = new Test1("王五");
  8. Console.ReadKey();
  9. }
  10. }
  11. public class Test1
  12. {
  13. public static int GetAge
  14. {
  15. get => 4;
  16. }
  17. public Test1()
  18. {
  19. }
  20. public Test1(string name, int age)
  21. {
  22. Console.WriteLine("姓名:" + name);
  23. Console.WriteLine("年龄:" + age);
  24. }
  25. public Test1(string name) : this(name, GetAge)
  26. {
  27. }
  28. }
  29. }

可以看到,这里的写法和 base 关键字类似,不过 base 是调用父类的构造函数。

执行后输出:

姓名:王五
年龄:4

六、扩展静态类方法

扩展方法的核心三要素是静态类,静态方法,和this参数

既在静态类中定义的静态方法,该方法的第一个参数带this

  1. namespace 计算3
  2. {
  3. public class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. string name = "233";
  8. name.TestString();
  9. int age = 3;
  10. age.TestInt();
  11. Console.ReadKey();
  12. }
  13. }
  14. public static class Espandi
  15. {
  16. public static void TestString(this string s)
  17. {
  18. Console.WriteLine("这是 string 的扩展方法:" + s);
  19. }
  20. public static void TestInt(this int t)
  21. {
  22. Console.WriteLine("这是 int 的扩展方法:" + t);
  23. }
  24. }
  25. }

在上面的 TestString 方法中,参数前面加 this ,可以理解为 给 string 类 添加了一个 静态方法 TestString,那么我们可以在其他的类中使用 string 类型变量直接调用这个方法了,而不需要使用 Espandi.TestString() 这种方式调用。

输出

上面的效果并不是那么明显,这个案例应该清晰很多

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Test4
  7. {
  8. internal class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. string name = "老王";
  13. name.Test(5);
  14. Console.ReadKey();
  15. }
  16. }
  17. public static class lib
  18. {
  19. public static void Test(this string name, int age)
  20. {
  21. Console.WriteLine("name:{0}, age:{1}",name,age);
  22. }
  23. }
  24. }

运行:

这个扩展方法,最大的好处就是调用的时候少写一个参数吧,想不出其他的好处了

结束

如果这个帖子对你有用,欢迎 关注 + 点赞 + 留言,谢谢

end

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/199869
推荐阅读
相关标签
  

闽ICP备14008679号