Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate array directly through object initializer [closed]

I have these 2 classes:

    class Customer
    {
        public string Name;
        public string City;
        public Order[] Orders;
    }
    class Order
    {
        public int Quantity;
        public Product Product;
    }

And then in the Main I do the following:

            Customer cust = new Customer
            {
                Name = "some name",
                City = "some city",
                Orders = {
                    new Order { Quantity = 3, Product = productObj1 },
                    new Order { Quantity = 4, Product = productObj2 },
                    new Order { Quantity = 1, Product = producctObj3 }
                }
            };

But I cannot initialize the array ... with a collection initializer. And I know that this, i.e., is possible string[] array = { "A" , "B" }; which looks the same to me...

Of course I could make separate objects of Order, put them in an array and then assign it toOrders , but I don't like the idea.

How can I achieve the clean and less-code-solution in this case?

like image 367
Milkncookiez Avatar asked Sep 28 '14 14:09

Milkncookiez


People also ask

How do you initiate an array of objects?

One way to initialize the array of objects is by using the constructors. When you create actual objects, you can assign initial values to each of the objects by passing values to the constructor. You can also have a separate member method in a class that will assign data to the objects.

How do you initialize an entire array with the same value?

int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index. The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list. Designated Initializer: This initializer is used when we want to initialize a range with the same value.

Can array objects be initialized?

Initializing Array Of Objects In an array of objects, we have to initialize each element of array i.e. each object/object reference needs to be initialized. Different ways to initialize the array of objects: By using the constructors. By using a separate member method.


2 Answers

C# does not provide JSON style notation for object initialization, because it is strongly statically typed language, that does not use aggressive type inference. You have to call array constructor(new Order[]) before using initializer code:

        Customer custKim = new Customer
        {
            Name = "some name",
            City = "some city",
            Orders = new Order[]{
                new Order { Quantity = 3, Product = productObj1 },
                new Order { Quantity = 4, Product = productObj2 },
                new Order { Quantity = 1, Product = producctObj3 }
            }
        };
like image 117
Eugene Podskal Avatar answered Oct 13 '22 22:10

Eugene Podskal


Jeroen and Eugene have provided some good options, but the truth is that you CAN use the syntax that you provided in your description if you use generic Lists, Collections and other types, but not with simple arrays.

So if you define your Customer class as:

class Customer
{
    public Customer()
    {
        Orders = new List<Order>();
    }

    public string Name;
    public string City;
    public List<Order> Orders;
}

You can use the syntax that you wanted to use in the first place:

Customer cust = new Customer
{
    Name = "some name",
    City = "some city",
    Orders = {
        new Order { Quantity = 3, Product = productObj1 },
        new Order { Quantity = 4, Product = productObj2 },
        new Order { Quantity = 1, Product = producctObj3 }
    }
};
like image 37
Faris Zacina Avatar answered Oct 13 '22 22:10

Faris Zacina