0

I created my class with a couple of properties.

And i was trying to use the List and create a constructor to send an object and have it creating the list with all the objects.

    public class List<MyObject>
    {
       public List<MyObject>(object x)
       {
           //Do Things here
       }   
    }

Is this possible?

3 Answers3

3

Would not be better to inherit List<>

for example :

   class InheritClass : List<ITest>
{
    public InheritClass(object parameter)
    {
        // do something
    }
}
class test
{
    public test()
    {
        InheritClass a = new InheritClass(new object());
        a.Add(new ITest);
    }
}
Mugsat
  • 48
  • 3
  • I could do that, but i dont know if i will need to extend all the methods like add and remove. Does it still work like the default ones? – Alexandre Leitão Jan 14 '16 at 10:51
  • Yes, you dont next to extent those method base `List` class provide you default functionally for it, just for your couple of properties this kind of defination for your class required – Ankush Madankar Jan 14 '16 at 10:53
0
public class MyObject : List<IListType>
{
   public MyObject(object x)
   {
       //Do Things here
   }   
}
Ankush Madankar
  • 3,689
  • 4
  • 40
  • 74
0

Why do this ?

  1. The list itself can take a 1..n of objects to create a list for you.
  2. You could use an Collection Initializer like: new List {1,2,3}

If you're looking for a means to create a list based on some domain requirements, I would look at Factorial patterns.

Jack Andersen
  • 1,059
  • 2
  • 8
  • 8