Alexandre Leitão
Alexandre Leitão

Reputation: 45

Custom List<MyObject> constructor

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?

Upvotes: 0

Views: 182

Answers (3)

Jack Andersen
Jack Andersen

Reputation: 1085

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.

Upvotes: 0

Ankush Madankar
Ankush Madankar

Reputation: 3834

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

Upvotes: 0

Mugsat
Mugsat

Reputation: 48

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);
    }
}

Upvotes: 3

Related Questions