您的位置:首页 > 其它

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

2014-03-18 22:43 246 查看
[code]
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 ? "男" : "女";
//    }
//}
}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐