List<T>.Contains determines equality by using the default equality comparer(System.IEquatable) which uses Object.Equals and Object.GetHashCode methods. In other words, your Custom class should either implement the System.IEquatable (not the System.IEqualityComparer!) interface or override the Object.Equals and Object.GetHashCode methods to support contains method.
Now, you must understand the below methods of System.IEquatable as those needs to be implemented in your custom class
Generics refers to the technique of writing the code for a class without specifying the data type(s) that the class works on.
C# is a strongly typed language. This implies that when using C# you should declare a type prior to storing data in it. Though this type safety helps enforce safety and interoperability between languages that target the managed environment, you as a developer are constrained to defining the type of any object you need to work with.
Generics enable you to work with typesafe data with no boxing and unboxing overhead. You can leverage generics to create typesafe collections, as well as classes and methods that can accept a type as a parameter. Such classes once declared can work with any type. This helps your code to be much more manageable, extensible, maintainable, and efficient. In this article we would explore generics and how they can be implemented using the C# programming language
In .NET some of the collection types such as Array, ArrayList, List<T> expose the Sort method that allows you to sort the collection. In order for those classes to sort the collection, there has to be a way for them to somehow compare objects with each other. The most basic Sort method usually does not require any arguments other than the collection itself in case of the static Array.Sort. In this case how does the framework know how to compare our objects? Is person1 greater than person2? Or maybe they are equal? This why we have IComparable & IComparer.
- IComparable as I’m comparable. which means I can be compared to another instance of the same type.
- IComparer as I’m a comparer, I simply compare which means I compare two instances.
The yield return and yield break keywords are shortcuts introduced in C# 3.0. They are designed to assist you in a method that returns IEnumerable <T>. If a calling function starts foreach-ing over this object the function is called again until it “yields”.
The easiest way understand code like this is to type in an example, set some breakpoints and see what happens.