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

来源:互联网 发布:c语言argv 编辑:程序博客网 时间:2024/04/30 05:28
 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 Test24     {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 student35     {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 No45         {46             get { return no; }47             set { no = value; }48         }49         private string name;50 51         public string Name52         {53             get { return name; }54             set { name = value; }55         }56         public void Say()57         {58             Console.WriteLine("姓名:"+Name+"; 编号:"+No);59         }60     }61 }

 


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>