Click here to Skip to main content
13,405,725 members (55,879 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


9 bookmarked
Posted 10 Jun 2011

Trick when using Array.Contains()

, 20 Jun 2011
Rate this:
Please Sign up or sign in to vote.
A trick when using Array.Contains()
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this?

string[] stuff = ....;
if (stuff.Contains("item"))

In .NET 3.5, this is possible out of the box (make sure you reference System.Core and include the System.Linq namespace) but if you try to run this code in .NET 2.0 or .NET 3.0, you will get errors. Nevertheless the .NET Framework 2.0 does provide a Contains() method on any Array object.

In the .NET Framework 2.0, System.Array implements the System.Collections.Generic.IList<T> interface. Unfortunately the implementation of the IList interface is provided at runtime, so we do not see the methods in Visual Studio and we cannot write array.Contains().

Instead, we have to cast the array to the appropriate IList interface:

string[] arr = new string[] { "RR US", "RR India", "RR UK" };
if (!((IList<string>)arr).Contains("India"))
     System.Console.WriteLine ("Correct! We are working with RR India");

The documentation explains it as follows:

In the .NET Framework version 2.0, the Array class implements the System.Collections.Generic.IList, System.Collections.Generic.ICollection, and System.Collections.Generic.IEnumerable generic interfaces. The implementations are provided to arrays at run time, and therefore are not visible to the documentation build tools. As a result, the generic interfaces do not appear in the declaration syntax for the Array class, and there are no reference topics for interface members that are accessible only by casting an array to the generic interface type (explicit interface implementations). The key thing to be aware of when you cast an array to one of these interfaces is that members which add, insert, or remove elements throw NotSupportedException.

So next time, you can save yourself from writing (unnecessary) code like this:

bool found =false;
foreach (string s in array)
    if (s.Equals("item"))
        found =true;
if (found)

Happy programming!!!!!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kiran Sonawane
Team Leader iNautix Technologies
India India
I am microsoft professional having about 8 years of experience on web technologies and working as TechLead. I am self starter guy and proficient in, web & wcf service. Try to learn something new everyday, this is how I work. In my 8 years of experience I worked with Microsoft technology, PHP, classic ASP, Cold Fusion and Ruby on Rails

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 Gud one Pin
kgsrr25-Jun-11 23:17
memberkgsrr25-Jun-11 23:17 
GeneralReason for my vote of 5 Interesting, but does it have some a... Pin
ivanicin21-Jun-11 10:57
memberivanicin21-Jun-11 10:57 
GeneralReason for my vote of 5 Helpful! Pin
w.a.g.i20-Jun-11 21:01
memberw.a.g.i20-Jun-11 21:01 
GeneralRe: Thanks!!! Pin
Kiran Sonawane21-Jun-11 1:58
memberKiran Sonawane21-Jun-11 1:58 
GeneralThanks to provied us to good use of Contains Pin
Sushma Sadafule13-Jun-11 3:15
memberSushma Sadafule13-Jun-11 3:15 
GeneralReason for my vote of 5 Good framework wise trick Pin
sachin10d12-Jun-11 23:06
groupsachin10d12-Jun-11 23:06 
Generalnice one , use of contains Pin
sarikangaikwad@gmail.com12-Jun-11 20:06
membersarikangaikwad@gmail.com12-Jun-11 20:06 
GeneralNice information, Thanks! Pin
Anarkali Bhaladar12-Jun-11 20:03
memberAnarkali Bhaladar12-Jun-11 20:03 
GeneralGood one, Thanks ! Pin
Member 429968212-Jun-11 19:59
memberMember 429968212-Jun-11 19:59 
GeneralNice one.... Thanks! Pin
RakeshMeena11-Jun-11 21:23
memberRakeshMeena11-Jun-11 21:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180221.1 | Last Updated 21 Jun 2011
Article Copyright 2011 by Kiran Sonawane
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid