Avatar ·

All possible ways to initialize arrays in C#: a complete syntax overview

Hello everyone! I've just started learning C# and I keep getting confused about the different ways to create arrays. There seem to be many options: new int[] {1,2,3}, just {1,2,3}, new[] {1,2,3}, and so on. I've seen a similar question on StackOverflow, but it's all in English, and I'd like a clear explanation in Russian.\n\nSpecifically, I'm interested in:\n1. What syntaxes exist for initializing arrays in C#?\n2. In which situations is each syntax best to use?\n3. Are there differences between one-dimensional, multidimensional, and jagged arrays?\n\nLet me give an example that confuses me. Why does this work:\n\n```csharp\nint[] arr1 = new int[3] { 1, 2, 3 };\nint[] arr2 = new int[] { 1, 2, 3 };\nint[] arr3 = new[] { 1, 2, 3 };\nint[] arr4 = { 1, 2, 3 };\n```\n\nBut this does not: