curly braces when define array

Leem.fin picture Leem.fin · Feb 2, 2012 · Viewed 23.8k times · Source

Regards to the following code:

int[] to = new int[] { text };

I understand it tries to define an array of integer, but What does the curly braces do in array definition?

Answer

Egor picture Egor · Feb 2, 2012

This is just a shortcut code to create an array with initial elements, the followings (which are equal):

    int[] to = new int[] { text };
    int[] to = { text };

can be substituted with

    int[] to = new int[1];
    to[0] = text;

Hope this helps.