How can I do the following using a inherited class?
Thanks
Jeff
namespace question
{
public class Dog
{
public string Breed { get; set; }
}
public class Dogs : List<Dog>
{
}
public class DoSomething
{
public static void HereWeGo()
{
Dog _brutus = new Dog();
Dog _rex = new Dog();
_brutus.Breed = "Basset Hound";
_rex.Breed = "Basset Hound";
Dogs _allDogs = new Dogs();
Dogs _allFoundDogs = new Dogs();
///this line here generates a compile time error because the .FindAll returns a
///List<Dog> not a collection Dogs. How can I use the list operations whilst using an inherited
///class please?
_allFoundDogs = _allDogs.FindAll(p => p.Breed == "Basset Hound");
}
}
}

1 answers
You' ll need to introduce a constructor in the Dogs class which calls the base class constructor. Here's one way you could do that:
answered 6 months ago by:
12813
18
Hi Vulpes, Thanks for that. As usual, the answer is obvious once you pointed it out. How did you get your post to have the colors and indenting like that? Thanks again. Jeff
18
I have added the following constructor. public Dogs(IEnumerable<Dog> collection) : base(collection) { } Thanks again Jeff
12813
For syntax highlighting etc. just place your code within [code] ... [/code] tags. This only works though in questions/answers, not in comments. It also doesn't work properly in IE8 (all blank lines seem to get removed, at least on my machine). So it's best to use FireFox or Chrome when accessing this site.
12813
Yes, it's a good idea to use IEnumerable<Dog> as the constructor parameter type so you don't have to call the ToArray() method when using a List<Dog> or LINQ query to populate a Dogs instance.