开发者

Object Initializer for object collections

开发者 https://www.devze.com 2022-12-08 15:29 出处:网络
I am wanting to find out if there is a way to initialize a List<T> where T is an object much like a simple collection gets initialized?

I am wanting to find out if there is a way to initialize a List<T> where T is an object much like a simple collection gets initialized?

Simple Collection Initializer:

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

Object Collection Initilizer:

 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 开发者_如何学Pythoncan do something like this?

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


If you look at the docs for collection initializers, it's all about the collection's Add method. Just subclass the closed generic List over your type and make an Add method with the naked parameters. Like

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 calling it looks like:

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


The best you can probably 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 could make it more generic, but how would you know which arguments should be bound 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 }
};


You can get close by creating your own collection which extends List<ChildObject> and provide your own add method:

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

You can then initialise it like this:

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


You can't do this without creating your own class derived from List<ChildObject> as per Lee's answer. It's unfortunate that extension methods aren't considered for collection initalizers, 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 }
        };
    }
}


The closest you can get to that is to create a parameterized constructor on your class that accepts those arguments. That won't get you all the way, 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号