Object Initializer for Object Collections

I want to find out if there is a way to initialize a List<T> , where T is an object , just as a simple collection is initialized?

Simple collection initializer:

 List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 

Object collection initializer:

  List<ChildObject> childObjects = new List<ChildObject> { new ChildObject(){ Name = "Sylvester", Age=8 }, new ChildObject(){ Name = "Whiskers", Age=2 }, new ChildObject(){ Name = "Sasha", Age=14 } }; 

The question is, how and if you can do something like this?

  List<ChildObject> childObjects = new List<ChildObject> { { "Sylvester", 8} , {"Whiskers", 2}, {"Sasha", 14} }; 
+6
c #
source share
5 answers

You cannot do this without creating your own class derived from List<ChildObject> according to Lee's answer. It is regrettable that extension methods are not taken into account for collection initializers, otherwise this would work:

 // This doesn't work, but it would if collection initializers checked // extension methods. using System; using System.Collections.Generic; public class ChildObject { public string Name { get; set; } public int Age { get; set; } } public static class Extensions { public static void Add(this List<ChildObject> children, string name, int age) { children.Add(new ChildObject { Name = name, Age = age }); } } class Test { static void Main(string[] args) { List<ChildObject> children = new List<ChildObject> { { "Sylvester", 8 }, { "Whiskers", 2 }, { "Sasha", 14 } }; } } 
+1
source share

If you look at the documents for collection initializers, all about the Add Add method. Just subclass the closed public list of your type and create an Add method with exposed parameters. how

 public class MyColl : List<ChildObject> { public void Add(string s1, int a1, int a2) { base.Add(new ChildObject(s1, a1, a2)); } } public class ChildObject { public ChildObject(string s1, int a1, int a2) { //... } } 

Then the call looks like this:

  static void Main(string[] args) { MyColl x = new MyColl { {"boo", 2, 4}, {"mang", 3, 5}, }; } 
+3
source share

The best you can do is something like this:

 public class MyListOfChildObjects : List<ChildObject> { public void Add( string name, int age ) { Add ( new ChildObject { Name = name, Age = age } ); } } var childObjects = new MyListOfChildObjects { { "Sylvester", 8 } , { "Whiskers", 2 }, { "Sasha", 14 } }; 

You can make it more general, but how do you know which arguments should be tied to each property?

 public class MyList<T> : List<T> { public void Add( params object[] arguments ) { // what order should I bind the values to? } } var childObjects = new MyList<ChildObject> { { "Sylvester", 8 } , { "Whiskers", 2 }, { "Sasha", 14 } }; 
+3
source share

You can get closer by creating your own collection that extends List<ChildObject> and provides its own add method:

 public class ChildObjectCollection : List<ChildObject> { public void Add(string name, int age) { this.Add(new ChildObject(name, age)); } } 

Then you can initialize it as follows:

 var childObjects = new ChildObjectCollection { { "Sylvester", 8} , {"Whiskers", 2}, {"Sasha", 1 } }; 
+3
source share

The closest thing you can do is to create a parameterized constructor in your class that takes these arguments. This will not help you, but you can at least do this:

 List<ChildObject> childObjects = new List<ChildObject> { new ChildObject("Sylvester", 8) , new ChildObject("Whiskers", 2), new ChildObject("Sasha", 14) }; 
0
source share

All Articles