Friday, February 1, 2008

How does Contains work in Generic List

We all know what contains mean for string operation, but there is a little ambiguity on what contains mean in a generic list.
Does it look for the correct item or does it look for all the Items which contains the entry.
Even though I know the answer intuitively I just thought lemme confirm that with a simple program.
The Program is as follows:
***************************************************************************************************
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main (string[] args)
{
List intList = new List();
intList.Add(100);
intList.Add(11);
intList.Add(111);
if(intList.Contains(1))
Console.WriteLine("one is present");
if(intList.Contains(11))
Console.WriteLine("11 is present");
if(intList.Contains(100))
Console.WriteLine("100 is present");
}
}
}
***************************************************************************************************
The output is :
11 is present
100 is present

So it looks for the *exact* Item

No comments: