<

Trick when using Array.Contains() C# 2.0

Published on
11,505 Points
5,105 Views
4 Endorsements
Last Modified:
Approved
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”))
{
    ...
}

Open in new window


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");
}

Open in new window


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;
            break;
        }
    }
    if (found)
    {
       ........
    }

Open in new window


Some of you guys might have an alternate solution for this.
For example

bool exists = Array.IndexOf(arr, "RR India") >= 0

Open in new window

No doubt this is an alternative solution solutions. This may differ in terms of performance. But, this is for c# 2.0 and as explained above .NET Framework 2.0 does provide a Contains() method on any Array object. So this may helpful for framework wise comparison.

Happy Programming!!!
4
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free