Problem using List.Contains()

J

Jeff

..NET 2.0

I'm trying to use List.Contains to determine if an object already exist in
the list. This Contains method always return false in my code

In my code a method gets a list of names, the method will return a generic
list containing only the distinct names. This fails in my code

Example:
public class Car
[Serializable]
private string _name;
public Car(string name) { _name = name; }
}

List<Car> cars = List<Car>();
Car car = new Car("Volvo");
list.Add(car);
car = new Car("Volvo");
if (!list.Contains(car))
List.Add(car) <<<<<<<<<<<< here is the problem in my code, Here
Contains didn't find the car("Volvo") already in the list, so this
if-statement fails and another car(volvo) object is added to the list

Maybe I instead should use Exists?

Any suggestions are welcome
 
M

Michael Huber

if (!list.Contains(car))

Here you are searching for a new Object, which is indeed not in your
List.

Remove the line " car = new Car("Volvo"); " and it'll worl :).

You have to override "GetHashCode" and "Equals" of your Car-Class in
order to get your example running.
 
G

Guest

I think the problem is related to what the list.Contains() does.

From the MSDN:

"The List class uses both an equality comparer and an ordering comparer.

Methods such as Contains, IndexOf, LastIndexOf, and Remove use an equality
comparer for the list elements. The default equality comparer for type T is
determined as follows. If type T implements the IEquatable generic
interface, then the equality comparer is the Equals method of that
interface; otherwise, the default equality comparer is Object.Equals."


So, you should implement the IEquatable interface on your Car object, and
tell it to compare the _name elements for the equality test.

e.g. (not a complete class !)


public class Car : IEquatable<Car>
{
...
protected string _name;

public string Name
{
get { return _name; }
...
}

#region IEquatable<Car> Members

public bool Equals(Car other)
{
bool result = false;

if( this.Name == other.Name )
{
result = true;
}

return result;
}

#endregion
}

HTH

Ged
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Jeff said:
.NET 2.0

I'm trying to use List.Contains to determine if an object already exist in
the list. This Contains method always return false in my code

In my code a method gets a list of names, the method will return a generic
list containing only the distinct names. This fails in my code

Example:
public class Car
[Serializable]
private string _name;
public Car(string name) { _name = name; }
}

List<Car> cars = List<Car>();
Car car = new Car("Volvo");
list.Add(car);
car = new Car("Volvo");
if (!list.Contains(car))
List.Add(car) <<<<<<<<<<<< here is the problem in my code, Here
Contains didn't find the car("Volvo") already in the list, so this
if-statement fails and another car(volvo) object is added to the list

Maybe I instead should use Exists?

Any suggestions are welcome

The Contains method checks if an item exists in the list. As this is a
list of references, it checks if the reference exists in the list.

The two Car objects that you create are separate objects, so they have
different references. The Contains method only looks for the reference,
so it doesn't find the existing object although it contains the same data.

Use a Dictionary<string, Car>, where the key is the name of the car.
 
J

Jon Skeet [C# MVP]

The Contains method checks if an item exists in the list. As this is
a list of references, it checks if the reference exists in the list.

No - it checks for items which are deemed to be equal. As Equals
hasn't been overridden, the default implementation is used, which is
just for reference equality. Your statement doesn't hold in general
though - a good example is a list of strings, where two separate
string objects with the same data are equal, and so List.Contains will
find one string given the other.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top