2

If i want to declare an array of same objects(I mean same constructor paramter values). can i do it in one shot.

In the below example If i want to create 4 box objects with same dimensions (3, 3, 2), I called the constructor 4 times. Can i do this in one shot?

class Program
{
    static void Main(string[] args)
    {
        Box b = new Box(3, 4, 7);
        Console.WriteLine(b.getVolume());

        Box[] matchBoxes = new Box[4];
        matchBoxes[0] = new Box(3, 3, 2);
        matchBoxes[1] = new Box(3, 3, 2);
        matchBoxes[2] = new Box(3, 3, 2);
        matchBoxes[3] = new Box(3, 3, 2);


        Console.ReadLine();

    }


}

class Box
{
    private int length;
    private int breadth;
    private int height;
    public Box(int l, int b, int h)
    {
        this.length = l;
        this.breadth = b;
        this.height = h;
    }
    public int getVolume()
    {
        return (this.length*this.breadth*this.height);
    }

}
5
  • Also, I cannot really see why you would need this in one shot. This is very clear for me and it isn't too complex. It would be more complex to do it in one shot Commented Nov 1, 2011 at 8:14
  • google.com/… Commented Nov 1, 2011 at 8:16
  • @JohnSaunders Lol, I really had no idea. Weird word Commented Nov 1, 2011 at 8:17
  • It means, "the characteristic of being broad" Commented Nov 1, 2011 at 8:18
  • @JohnSaunders It's also a synonym to "Width". Never heard it been used tho Commented Nov 1, 2011 at 8:20

7 Answers 7

3

That depends. Since this is a class you would need to call new 4 times if you want to get 4 different objects. However, your box looks to be immutable; if you are happy to use the same object 4 times (which might be reasonable here), you could use:

var box = new Box(3,3,2);
var matchBoxes = new[] {box,box,box,box};

If that is the entirety of your Box type, you might also want to consider making it a struct (immutable, small, value-esque - definitestruct candidate). Then it is moot : it will be a different value in each position. The construction could be the same as above, though.

Sign up to request clarification or add additional context in comments.

6 Comments

Your array will contain 4 identical objects - i.e. one object 4 times. I think this is not what Preveen wants.
@Karel please read what I wrote; I noted that the type is immutable, so it might be reasonable to use the same instance 4 times. I also emphasised that if you want a different instance each time, you need to call new each time.
I understand you, but Praveen asked the question and he was not talking about "immutability".
@KarelFrajtak Does it really matter what he was talking about? As long as it's applicable (which it is), I would say it's fine
@Karel I provided options. In the question asked, with the sample shown, it is entirely reasonable to re-use the instance, which I have illustrated. Regardless of the OP mentioning immutability, that still applies here and it is right to note it. I also mention that if you want different objects, you'd need to use new each time. Please: what specifically are you criticizing? It is often important to look beyond simply what the OP is asking in words, but to look at the context to understand the full scenario. I feel I have done so.
|
1

If you want to create 4 new objects, you have to call constructor 4 times. If you do not want to type it, use cycle:

for(int i = 0; i < 4; i++) matchBoxes[i] = new Box(3, 3, 2); 

Comments

1

Many of the answers are spot on (use a struct, use a loop);

Just to be complete:

    Box[] matchBoxes = Enumerable.Repeat(new Box(3, 3, 2), 4).ToArray();

Note as Marc Gravell stated that this will NOT give separate copies of the Box, unless it is a valuetype.

You could even make it more general:

    var generator   = Enumerable.Repeat(new Box(3, 3, 2));

    // ....
    var fourBoxes   = generator.Take(4);
    var twentyBoxes = generator.Take(20);

Comments

1
var matchBoxes = Enumerable.Repeat(new Box(3, 3, 2), 4).ToArray();

Comments

0

Nope. each object needs to be created separately. Use a loop :)

Comments

0

Why not using a loop, with parameters to be passed in constructors?

Comments

0

I hope this solve your problem,

Box[] matchBoxes = new Box[]{ new Box(3, 3, 2), new Box(3, 3, 2), new Box(3, 3, 2), new Box(3, 3, 2)};

1 Comment

Mmmm. Some might say it became worse. It did remove the redundant specification of the array length, I guess

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.