First look at the extension of ScottGu in:
Call Example 1:
Call Example 2:
Original address: New "Orcas" Language feature:extension Methods (http://weblogs.asp.net/scottgu/archive/2007/03/13/ new-orcas-language-feature-extension-methods.aspx)
Many of the extension methods are also used as examples of "in", but few people think about it any further. Personally feel this in expansion is not thorough enough to look at the following code:
public static void Example1()
{
bool b1 = 1.In(new int[] { 1, 2, 3, 4, 5 });
bool b2 = "Tom".In(new string[] { "Bob", "Kitty", "Tom" });
}
//ScottGu In扩展
public static bool In(this object o, IEnumerable c)
{
foreach (object i in c)
{
if (i.Equals(o)) return true;
}
return false;
}
To declare an array (or set) every time you use in, it's a bit cumbersome, and it should be simpler to call like this:
public static void Example2()
{
bool b1 = 1.In(1, 2, 3, 4, 5);
bool b2 = 1.In(1, 2, 3, 4, 5, 5, 7, 8);
bool b3 = "Tom".In("Bob", "Kitty", "Tom");
bool b4 = "Tom".In("Bob", "Kitty", "Tom", "Jim");
}
How does it feel, is it a bit like in SQL?