Linq为我们提供了许多扩展方法,方便我们对数据源进行操作(Where,Select...)。即使你不了解算法,也能使用Linq当回牛人。扩展方法本质并不是什么高深的技术,说白了就是一个Static静态方法。 声明扩展方法步骤:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Linq
{
public static class 扩展方法Helper
{
public static string ToMyUpper(this string helper)
{
return "\""+helper.ToUpper() + "\"";
}
public static string Quoted(this string helper,string a,string b)
{
return a + helper + b;
}
public static bool IsNumber(this string helper)
{
int i;
return int.TryParse(helper,out i);
}
public static string ToChineses(this bool helper)
{
return helper ? "真" : "假";
}
public static int CreateMan(this Person helper)
{
Person one = new Person { Age=18,Name="Eyes"};
return one.Age;
}
}
public class Person
{
public int Age { get; set; }
public string Name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Linq
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
Console.WriteLine(p.Name.IsNumber().ToChineses());
Console.ReadKey();
}
}
}
系列完善中,请持续关注...