2

I am currently teaching myself c# but am confused by the following syntax:

new Deck( new Card[] {} )

where the Deck constructor looks like this:

public Deck(IEnumerable<Card> initialCards)

what does the {} bit mean?

3 Answers 3

10

It is an array initializer, and in this instance initializes an empty array.

It can also be used as follows:

int[] bling = new [] { 1, 2, 3 };

or

int[] bling = { 1, 2, 3 };
Sign up to request clarification or add additional context in comments.

Comments

0

It is a collection initializer, can be used as follows

new Card[] {
    new Card(),
    new Card(),
    new Card()
};

To initilise an array of cards of length 3 containing three card objects. As you have it, it will be an empty array.

1 Comment

I think the term is array initializer. Collection initializers work by calling Add, which doesn't make sense for an array.
-5

It initialize an empty array.If you have a class like this :

class student {
    private string name;
    private int age;
}

you can initialize this :

student a = new student { name = "a", age = 10};

Sorry for my mistake above. You can initilize an array of student :

student[] students = new student[] {new student {name = "a", age = 10}, new student {name = "b", age = 20 }};

1 Comment

you still have a stray colon.

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.