扩展方法的实例应用

给People添加一个扩展方法,打印出people的所有属性

View Code
 1 using System;
 2 public class ExtendMethod
 3 {
 4     public static void Main()
 5     {
 6         People p = new People();
 7         p.ToValue("小小",20,"");
 8     }
 9 }
10 public static class Extend
11 {
12     public static void ToValue(this People pp,string name,int age, string sex)
13     {
14         Console.WriteLine(name+"   "+age+"   "+sex);
15     }
16 }
17 public class People
18 {
19     public string Name
20     {get;set;}
21     public int Age
22     {get;set;}
23     public string Sex
24     {get;set;}
25 }

 

你可能感兴趣的:(方法)