Let's take a look at ScottGu's In extension:
Call Example 1:
Call Example 2:
Original article address: New "Orcas" Language Feature: Extension Methods
Many extension methods use "In" as an example, but few people think about it further. I personally feel that this In extension is not thorough enough. refer to 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 extension
Public static bool In (this object o, IEnumerable c)
{
Foreach (object I in c)
{
If (I. Equals (o) return true;
}
Return false;
}
Every time you use In, you need to declare an array (or set), which is a little troublesome. It should be simpler to call it like below:
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 );