.net 扩展方法

复制代码
class ExtensionMethods2    
{

static void Main()
{
int[] ints = { 10, 45, 15, 39, 21, 26 };
var result
= ints.OrderBy(g => g);
foreach (var i in result)
{
System.Console.Write(i
+ " ");
}
}
}
复制代码
namespace ExtensionMethods
{
public static class MyExtensions
{
public static int WordCount(this String str)
{
return str.Split(new char[] { ' ', '.', '?' },
StringSplitOptions.RemoveEmptyEntries).Length;
}
}
}
复制代码

要使用时只需要要:
string s = "Hello Extension Methods";
int i = s.WordCount();
复制代码
posted @   湛然  阅读(189)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示