ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

In

Allows you to compare a value to a list of values analogous to the 'In' statement in sql. This makes for a very friendly syntax that is (IMHO) superior to a list of 'or' clauses. Instead of : if (s=="John" || s=="Peter" or s=="Paul") one can write if (s.In("John","Paul","Peter"))

Source

public static bool In<T>(this T source,params T[] list)
{
   return list.ToList().Contains(source);
}

Example

string s="John";

if (s.In("John","Paul","Peter"))
{
   Debug.WriteLine("Are you an apostle?");
}

Author: C.F. Meijers

Submitted on: 3 mrt. 2010

Language: C#

Type: System.Object

Views: 5523