0

If I subclassed list, and added a property...

class MyList : List<MyType>
{
    public int MyProperty {get;set;}
}

or

class MyCollection<T> : ICollection<T>
{
    public int MyProperty {get;set;}
}

I can initialize the property:

var l = new MyList() {MyProperty = 1};

Or I can initialize the list:

var m = new MyList() {new MyType(),new MyType()};

Can I initialize both the list and properties of the list?

Jeremy
  • 44,950
  • 68
  • 206
  • 332
  • 6
    Deriving from a `List` is usually the wrong approach.. https://stackoverflow.com/questions/21692193/why-not-inherit-from-listt – TheGeneral Jan 23 '19 at 06:33
  • Why not derive from `IEnumerable` or even `ICollection`? Usually the collection classes implement interfaces as mentioned. – Tetsuya Yamamoto Jan 23 '19 at 06:35
  • @TheGeneral - This is not exactly my implementation. I'm just using this example for illustration purposes. I just want to know if it is possible to initialize a collection and a property at the same time. – Jeremy Jan 23 '19 at 15:17

0 Answers0