使用扩展方法,向现有类型“添加”方法

来源:互联网 发布:沈阳纳森网络靠谱吗 编辑:程序博客网 时间:2024/05/24 07:14
 public class Student    {        public bool GetSex()        {            return false;        }    }    class Program    {        static void Main(string[] args)        {            Student student = new Student();            Console.WriteLine(student.GetSexString());        }    }    public static class StudentExtension    {        public static string GetSexString(this Student student)        {            return student.GetSex() == true ? "男D" : "女?";        }    }

0 0