Add note about collection expressions. (#40864)

* Add note about collection expressions.

Fixes #38712

Note that these examples all use collection expressions.

* Update docs/csharp/language-reference/builtin-types/arrays.md

Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>

* show both examples in note.

---------

Co-authored-by: Genevieve Warren <24882762+gewarren@users.noreply.github.com>
This commit is contained in:
Bill Wagner 2024-05-13 16:54:31 -04:00 коммит произвёл GitHub
Родитель eaeb11082e
Коммит 44857895ad
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
1 изменённых файлов: 10 добавлений и 0 удалений

Просмотреть файл

@ -41,6 +41,16 @@ The following example creates single-dimensional, multidimensional, and jagged a
:::code language="csharp" source="./snippets/shared/Arrays.cs" id="DeclareArrays":::
> [!IMPORTANT]
> Many of the examples in this article use [collection expressions](../operators/collection-expressions.md) (which use square brackets) to initialize the arrays. Collection expressions were first introduced in C# 12, which shipped with .NET 8. If you can't ugrade to C# 12 yet, use `{` and `}` to initialize the arrays instead.
>
> ```csharp
> // Collection expressions:
> int[] array = [1, 2, 3, 4, 5, 6];
> // Alternative syntax:
> int[] array2 = {1, 2, 3, 4, 5, 6};
> ```
## Single-dimensional arrays
A *single-dimensional array* is a sequence of like elements. You access an element via its *index*. The *index* is its ordinal position in the sequence. The first element in the array is at index `0`. You create a single-dimensional array using the [new](../operators/new-operator.md) operator specifying the array element type and the number of elements. The following example declares and initializes single-dimensional arrays: