向string,int,类对象等中扩展方法

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string s = "sss";
13             s.Res();
14             /**************************/
15             student stu = new student("11013141","二货");
16             stu.Say();
17             stu.sayhello();
18         }
19     }
20     /// <summary>
21     /// 扩展方法写在一个静态类中
22     /// </summary>
23     public static class Test
24     {
25         public static void Res(this string s)
26         {
27             Console.WriteLine(s);
28         }
29         public static void sayhello(this student s)
30         {
31             Console.WriteLine(" 编号:" + s.No+";姓名:" + s.Name);
32         }
33     }
34     public class student
35     {
36         //public student() { }
37         public student(string no,string name)
38         {
39             this.No = no;
40             this.Name = name;
41         }
42         private string no;
43 
44         public string No
45         {
46             get { return no; }
47             set { no = value; }
48         }
49         private string name;
50 
51         public string Name
52         {
53             get { return name; }
54             set { name = value; }
55         }
56         public void Say()
57         {
58             Console.WriteLine("姓名:"+Name+"; 编号:"+No);
59         }
60     }
61 }

 

posted on 2013-05-29 09:28  程序猴chengxuhou.com  阅读(489)  评论(0编辑  收藏  举报

导航