0

I want to write a List on my own. So I have a class List and a class ListItem. What I now want to do is to create differente classes, for example the class car. I would like to deduce the class car from the class ListItem so that I can create a List of cars. Now the Class List shall have different methodes like the one C# gives you. I wrote an Add and a Count methode and both work. Here comes the tricky part... I want to write a Funktion to Find an Item in the List. The problem is that I can´t just compare them because 1. I don´t know what the Objekt will look like and what members it will have. 2. I don´t want to compare the characteristics of the class ListItem because they aren´t relevant to the real Object, they are just relevant for the List.

My Idea is, to Create a List Class for each object so in my example it would be a carList which is deduced from the List Class. This way I could write a Methode that would know what to compare because it would know the ListItem Object. Now I don´t want to create a list class for every Object I want to have a list of. There must be a different way sice c# also gives you the same list class. Can anyone help?

J.Doe
  • 57
  • 1
  • 9
  • 1
    Your question was "Can anyone help?" - the answer is plainly "Yes, someone can help". But please share what you have to far, helping yourself by making helping you easier. – Dietz Oct 06 '15 at 11:48
  • @fubo I don´t know what you mean... Of course I give the object to search for as an overload to my function! – J.Doe Oct 06 '15 at 11:49
  • Can you give us a proposed method signature for the `Find` method? Will there also be a `Contains` method. – Gusdor Oct 06 '15 at 11:51
  • 2
    Also, why not just use the provided generic `List` Class? You'll sidestep most of your issues trivially. – SWeko Oct 06 '15 at 11:54
  • @SWeko may be it's a homework ... – Xaruth Oct 06 '15 at 11:56

1 Answers1

-1

You should override Equals (inherited from object) on your Car-class:

class car
{
    public override bool Equals(object other) {
        var o = other as car;
        if ( o == null ) return false;

        if this.myProperty = o.myProperty return true;

        return false;
    }
}

The list will then use this implenentation for detecting if two cars are equal. Within your Find-method you can now simply call for every item in the list if it equals the searched one:

if (currentItemInList.Equals(searchItem)) // ...

EDIT: When implementing Equals for any class you should always also implement GetHashCode in case that objects of your class are stored in hashed containers like Dictionary<TKey,TValue>. If you want to read further see MSDN for GetHashCode and Equals

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
  • If you write the whole list implementation yourself you can actually name the comparison methods any way you like, because it's you who calls them. If it could happen that your objects end up in generic containers though you should probably implement `IEquatable`, in addition to `object.Equals()`. Cf. https://msdn.microsoft.com/en-us/library/ms131187%28v=vs.110%29.aspx. You should for sure also implement `GetHashCode()`. – Peter - Reinstate Monica Oct 06 '15 at 11:57
  • @PeterSchneider Of course you´re right, however I tried to keep it simple. I added the HashCode also. – MakePeaceGreatAgain Oct 06 '15 at 12:01
  • Hmm... I thought that `GetHashCode()` is only important for Dictionaries and possibly other hashed containers, but not for general equality tests. – Peter - Reinstate Monica Oct 06 '15 at 12:06
  • @PeterSchneider Nope, see http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overridden for why implementing getHasCode also – MakePeaceGreatAgain Oct 06 '15 at 12:10
  • A tiny comment: if you're going to use `is` then cast, you may as well use `as` then check for `null`. – Wai Ha Lee Oct 06 '15 at 12:19
  • But Marc Gravell's answer says exactly that: *"if your item will be used as a key in a dictionary..."* or other containers which use a hash code. `GetHashCode()` is by no means called whenever `Equals()` is called. That would be costly because computing a hash code can be just as expensive as testing for equality. It's just that the hashed containers use the hash code for quick retrieval, but for that must rely on the hash code's logical connection to equality. – Peter - Reinstate Monica Oct 06 '15 at 12:37
  • ... you're almost there - but you made it cast instead of using `as`, which can result in an `InvalidCastException` for non-`Car` instances. I've edited your answer to resolve this. – Wai Ha Lee Oct 06 '15 at 12:43
  • 1
    @WaiHaLee Oh dear, ya you´re right, sorry. This happens when coding faster then thinking. – MakePeaceGreatAgain Oct 06 '15 at 12:52
  • `Equals(object)` vs. `Equals(type)` has nothing to do with `GetHashCode()`. – Peter - Reinstate Monica Oct 06 '15 at 13:00
  • @PeterSchneider oops, whrong post, ya you´re right. Looking for the correct one. Here is it: "It is recommended that any class that overrides Equals also override System.Object.GetHashCode." – MakePeaceGreatAgain Oct 06 '15 at 13:03
  • I took the liberty to edit the post so that the role of `GetHashCode()` is clarified. – Peter - Reinstate Monica Oct 06 '15 at 13:20