c#this关键字
1. 代表当前类的对象
class Father
{public int Age { get; set; }public string Name { get; set; }public Father(int age, string name){this.Age = age;this.Name = name;}public void Test(){Console.WriteLine($"name:{this.Name },age:{this.Age }");}}
调用:
Father father = new Father(100, "小明");
father.Test ();
father = new Father(50, "小李");
father.Test();
输出:
name:小明,age:100
name:小李,age:50
2. 在同一个类中在一个构造函数中调用另一个构造函数
class Father
{public int Age { get; set; }public string Name { get; set; }public int Height { get; set; }public Father(int age, string name){this.Age = age;this.Name = name;}public void Test(){Console.WriteLine($"name:{this.Name },age:{this.Age }");}
}
如果要传入三个参数,那么可以多加一个构造函数,如下:
class Father
{public int Age { get; set; }public string Name { get; set; }public int Height { get; set; }public Father(int age, string name){this.Age = age;this.Name = name;}public Father(int age, string name, int height){this.Age = age;this.Name = name;this.Height = height;}public void Test(){Console.WriteLine($"name:{this.Name },age:{this.Age }");}}
也可以使用this来简化代码,如下:
class Father
{public int Age { get; set; }public string Name { get; set; }public int Height { get; set; }public Father(int age, string name){this.Age = age;this.Name = name;}public Father(int age, string name, int height):this (age ,name){this.Height = height;}public void Test(){Console.WriteLine($"name:{this.Name },age:{this.Age }");}}
3. 为某个类添加扩展方法
class Father
{public int Age { get; set; }public string Name { get; set; }public int Height { get; set; }public Father(int age, string name){this.Age = age;this.Name = name;}public Father(int age, string name, int height) : this(age, name){this.Height = height;}public void Test(){Console.WriteLine($"name:{this.Name },age:{this.Age }");}}
static class FatherExtension
{public static void Eat(this Father father )//指定为Father类添加扩展方法,无参方法{Console.WriteLine("吃饭");}public static void Run(this Father father,bool isRun)//添加有参方法{Console.WriteLine($"{isRun }");}
}
调用:
Father father = new Father(100, "小明");
father.Eat();
father.Run(true );
什么时候需要使用扩展方法,个人认为有两种情况,
- 某个类(非静态类)的源代码我们看不到,但是又想为这个类添加方法,这时候可以使用扩展方法,比如系统自带的一些类
- 为接口添加一个方法,并且这个方法对于实现接口的这些类来说方法都是一样的,如果在接口中添加这个方法的定义,那么我们就需要除了在接口中增加方法的定义以外,还需要在实现接口的每个类中写一个一模一样的方法
比如有个IPerson接口,里面有个MyAge的方法,然后我们现在想添加一个MyName的方法,并且MyName的内容一模一样,如果不使用扩展方法的话,则如下:
添加MyName方法前:
interface IPerson{void MyAge();}class Chinese : IPerson{public void MyAge(){Console.WriteLine(10);}}class American : IPerson{public void MyAge(){Console.WriteLine(5);}}
可以看到在Chinese 类中、American 类中都添加了一个MyName的方法,如果接口的实现类有100个的话,那么我们还要添加100个一模一样的方法,这样无疑是重复的工作,如果通过通过扩展方法对接口进行扩展,则如下面的代码所示:
interface IPerson{void MyAge();}class Chinese : IPerson{public void MyAge(){Console.WriteLine(10);}}class American : IPerson{public void MyAge(){Console.WriteLine(5);}}static class IpersonExtension{public static void MyName(this IPerson person ){Console.WriteLine("小明");}}
从上面的代码可知,我们可以不更改接口和接口的实现类的代码,然后另外添加一个静态类,然后添加静态方法对接口进行扩展,同样实现了功能,但是减少了很多重复代码。
4. 索引器中用于访问集合元素
public enum NameOrder{First,Second}public class Name<T>{public Name(T firstName, T lastName){this.FirstName = firstName;this.LastName = LastName;}public T FirstName { get; set; }public T LastName { get; set; }public T this[NameOrder index]{get{switch (index){case NameOrder.First:return FirstName;case NameOrder.Second:return LastName;default:return default;}}}}
调用:
Name<string> name = new Name<string>("四", "李");
string firstName = name[NameOrder.First];
Console.WriteLine(firstName );