使用扩展方法(this 扩展类型)

namespace ConsoleApp_UseExtendWays
{
class Program
{
static void Main(string[] args)
{
Student s = new Student();
Console.WriteLine(s.GetSexString());
Console.WriteLine(s.GetAge());
Console.ReadKey();
}
}
public static class StudentExtension
{
public static string GetSexString(this Student argStudent)//扩展方法 this 扩展类型
{
return argStudent.GetSex() == true ? "男" : "女";
} public static int GetAge(this Student ss)
{
return ss.Age;
}
}
public class Student
{
public bool GetSex()
{
return false;
} public int Age = 12;
} //public static class StudentConverter
//{
// public static string GetSexString(Student argStudent)
// {
// return argStudent.GetSex() == true ? "男" : "女";
// }
//}
}
上一篇:Linq常用List操作总结,ForEach、分页、交并集、去重、SelectMany等


下一篇:C#页面缓存设置