| IsIn type extension |
|
|
|
| Technology - Coding | |||||||||
| Written by Romeo Dumitrescu | |||||||||
| Wednesday, 21 January 2009 14:41 | |||||||||
|
This small type extension checks wether a <T> value is in the checklist. Very useful... public static bool IsIn<T>(this T value, params T[] checkList) {bool result = false; bool emptyList = (checkList == null || checkList.Length <= 0); if (emptyList && Equals(value, default(T))) result = true;else if (!emptyList) foreach (T item in checkList) if (value.Equals(item)) { result = true; break;} return result;}
Example: string toSearch = "search"; bool found = toSearch.IsIn("This", "extension", "is", "used", "to", "search", "for", "values"); Due to the generic, this extension is not type bound and can be used with all value types. For other objects, different from primitives, you can extend this method with an IEquality comparer.
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."
|





