LINQ: The overloaded Contains Operator

A LINQ Tutorial

As part of my continuing (and occasional) series of mini-tutorials on LINQ this posting will consider the overloaded Contains operator. This operator allows you to check whether a list contains a given value, as illustrated in the following code that you can drop into LinqPad or wrap within a console application:

var someList = Enumerable.Range(1,10);

Console.WriteLine(
 "someList contains 6? {0}, someList contains 12? {1}",
 someList.Contains(6), someList.Contains(12));

 

The output for this is

someList contains 6? True, someList contains 12? False

As noted, the Contains operator is overloaded, and you are free to pass in your own comparison method to facilitate comparing items of different types.

To see this at work, create a Console Application in Visual Studio. Within the application create a Person class and derived from that, a Student class:

class Person
{
    public int ID { get; set; }
    public string FullName { get; set; }
}

class Student : Person
{
    public string Major { get; set; }
}

 

Next, create a class derived from IEqualityComparer, which will establish whether two People are equal or not using whatever you choose as the criteria. You might match FullName fields or, more likely, ID:

class StudentToPersonEquals : IEqualityComparer<Person>
 {
     public bool Equals( Person x, Person y )
     {
         if (x.ID == y.ID)
             return true;
         else
             return false;
     }

     public int GetHashCode( Person obj )
     {
         return obj.GetHashCode();
     }
 }

 

With this in place, you can instantiate a number of Person and Student objects, and place them in a collection:

var people = new List<Person>();

var PaulBetts = new Student() 
{ 
    FullName =  "Paul Betts", 
    ID = 1, 
    Major = "Computer Science" 
};
people.Add(PaulBetts);

people.Add( new Person() 
    { ID = 2, FullName = "Jesse Liberty" } );

people.Add( new Student() 
    { 
        ID = 3, 
        FullName = "George Washington", 
        Major = "Favorite on Fawlty Towers" 
    } );

people.Add( new Student() 
    { 
        ID = 4, 
        FullName = "John Adams", 
        Major = "History" 
    } );


 

You can now use an instance of your StudentToPersonEquals class as the comparer for the Contains operator,

Console.WriteLine ("People contains Paul Betts? {0}",
    people.Contains(PaulBetts, new StudentToPersonEquals() ));

 

This allows you to pass in an instance of the Student class and have it identified by the Contains operator based on the criteria specified in your comparison class.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Data, Linq, Mini-Tutorial and tagged . Bookmark the permalink.

10 Responses to LINQ: The overloaded Contains Operator

  1. Pingback: Linksammlung 12/6/2011 | Silverlight, WPF & .NET

Comments are closed.