Poll

Which programming language do you prefer?
 
Feed Your Need!

About Me

My name is Romeo Dumitrescu

I am a Software Developer for Macadamian Technologies

Downloads

No Documents

Who's Online

We have 5 guests online
IsIn type extension PDF Print E-mail
User Rating: / 0
PoorBest 
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.

Comments
Add New Search
Write comment
Name:
Email:
 
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch::(:shock::X:side:
:):P:unsure::woohoo::huh::whistle:;):s:!::?::idea::arrow:
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."