C
C#6d ago
Faker

{} vs [] when initializing an array in C#

Hello guys, is there a difference when we use {} to initialize an array or if we use [] ? I read that the [] is known as a collection expression.
C#

string[] orders = {"B123", "C234", "A345", "C15", "B177", "G3003", "C235", "B179"};

vs

string[] orders = ["B123", "C234", "A345", "C15", "B177", "G3003", "C235", "B179"];
C#

string[] orders = {"B123", "C234", "A345", "C15", "B177", "G3003", "C235", "B179"};

vs

string[] orders = ["B123", "C234", "A345", "C15", "B177", "G3003", "C235", "B179"];
4 Replies
Jimmacle
Jimmacle6d ago
the second one is a newer way to do it, but they do the same thing in these examples the [] way can be used with lists and other collection types as well
Faker
FakerOP6d ago
yep I see, it's better to just stick with the newer syntax?
Jimmacle
Jimmacle6d ago
it doesn't really matter, but if you want to be consistent then the newer syntax can be used in more places
Faker
FakerOP6d ago
ok noted, ty

Did you find this page helpful?