They are not the same thing. A FooList is a List.
Up vote 4 down vote favorite 1 share g+ share fb share tw.
This is probably me just remembering things completely backwards, but I'd like to know more about what I'm doing wrong... I have declared a class to be nothing more than a direct inheritance from a generic list (done to simplify naming), something like this: public class FooList : List {} now in another method completely separate from this class, I am trying to return an instance of this class, however I want to filter the class based on a criterion, so I'm using a lambda expression: var list = new FooList(); // imagine this fills it with different items var filtered = list. FindAll(c => c. Something == "filter criteria"); now according to the FindAll method, this SHOULD return a ListFoo.
However, I want to return this object as a FooList, not a ListFoo. Do I have to create a new instance of FooList and copy the items from the ListFoo? If so, why?
Why can't I convert a List to a FooList directly, since they are the same object? If this CAN be done, how do I do it? Many thanks!
C# inheritance generic-list link|improve this question edited Sep 15 '10 at 21:48Timwi20.6k23085 asked Sep 15 '10 at 21:38Josh826410 77% accept rate.
They are not the same thing. A FooList is a List but a List, which is being returned by the FindAll() function inherited from List, is not a FooList. You will need to construct a new FooList.
You could do something like create a Constructor for FooList that takes a IEnumerable object like this: class FooList : List { public FooList(IEnumerable l) : base(l) { } } Or you could also do something like this: var list = new FooList(); var filtered = list. FindAll(c => c. Something == "filter criteria"); var newList = new FooList(); newList.
AddRange(filtered); return newList; However as mentioned in some of the other answers, if you are not adding any functionality then you should just create an alias with the using keyword.
This is about what I figured, but to me it just seems like it SHOULD work, but I guess I do understand why it doesn't. I knew I had it backwards. Is there a way to initialize the FooList from the ListFoo or am I going to instead add each one individually?
Btw, THANKS! – Josh Sep 15 '10 at 21:46 2 On the plus side List has a constructor overload that takes an IEnumerable argument, so creating a new FooList and copying the values should be easy. – PhilB Sep 15 '10 at 21:48 @PhilB +1 For beating me while I was away coding :) – linuxuser27 Sep 15 '10 at 21:53 very very cool, I learned something new!
I love my job :) thanks everyone for the help! – Josh Sep 15 '10 at 22:00.
Your problem is that you are using the wrong tool for the job. Inheritance is not a mechanism for simplifying naming; inheritance is a mechanism primarily designed for (1) modeling "is a kind of" relationships between classes of business domain objects, and (2) enabling the sharing and specialization of implementation details amongst related types. If you want to simplify naming then the right tool for the job is to put using FooList = System.Collections.Generic.
List; at the top of every file.
Interesting, I've never heard of this before, I will look into it! There was originally supposed to be added functionality to this, but we never got there. I will certainly keep this in mind for the future, thanks!
– Josh Sep 15 '10 at 22:00 +1 That's it. There is no necessary for OP to create a ListFoo class inherited from List in order to make the code shorter. Using resolves the root problem.
– Danny Chen Sep 16 '10 at 2:39.
Don’t declare a new class FooList. Just use an alias. At the top of your source file, write: using FooList = System.Collections.Generic.
List; Now you can write FooList everywhere and it will be treated to be exactly identical to List.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.