I have this example class:
class MyClass
{
public string name;
public string name2;
}
and I want to make a list of MyClass
like List<MyClass> myClass = new List<MyClass>();
but I was told that a better approach would be to make some kind of a "Collection". This is how my List looks like now:
class MyClassCollection : List<MyClass>
{
}
As I understand it is better because I can now use my own methods and for example override .Add()
.
So my "By the way"-question is: Is that true?
I created MyClassCollection myClassCollection = new MyClassCollection();
and I added some MyClass
objects to it. But how do I sort it by alphabetial order of name
?
If it was a list I could sort it with this:
List<MyClass> myList = new List<MyClass>();
myList.Add(myClassObject1);
myList.Add(myClassObject2);
myList = myList.OrderBy(x => x.name).ToList();
But:
MyClassCollection myClassCollection = new MyClassCollection();
myClassCollection.Add(myClassObject1);
myClassCollection.Add(myClassObject2);
myClassCollection = myClassCollection.OrderBy(x => x.name).ToList();
does not work, as the ToList()
method returns an object of type List<MyClass>
and not a MyClassCollection
object.