标签:
static class CommonHelper { public static string TransferStrToUpper(this string str) { return str.ToUpper(); } } class Program { static void Main(string[] args) { string str = "abcDEfg"; string str1=CommonHelper.TransferStrToUpper(str); string str2=str.TransferStrToUpper(); Console.WriteLine("str1={0},str2={1}",str1,str2); Console.ReadKey(); } }
变量str1,str2都能成功转成大写。从表面上看变量str有一个TransferStrToUpper方法,但本质上其仍然是静态方法。
sealed class SayLove { public string SayLoveYou(string personName) { return personName + ",I Love you."; } } static class SayLoveExtend { public static string ExpressLove(this SayLove sl,string girlFirend) { return sl.SayLoveYou(girlFirend)+"Marry me."; } } class Program { static void Main(string[] args) { string str = "Lily"; SayLove sl = new SayLove(); string str1=sl.ExpressLove(str); Console.WriteLine(str1); Console.ReadKey(); } }
程序输出:
标签:
原文地址:http://www.cnblogs.com/kungge/p/4817884.html