Skip to content

FR CSharp Operator Collection Expression

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Collections et diffusion

Manuel C# · Guide pratique

L'expression de collection nécessite un type de cible ; spread ajoute des éléments d’une autre séquence. Ne confondez pas propagation et portée.

Exemple simple (Example.cs)

Enregistrez cet exemple séparément dans Example.cs. Exécutez-le indépendamment des autres exemples de cette page.

Example.cs:

// PROJECT-EXAMPLE
int[] first = [1, 2];
int[] all = [.. first, 3];
System.Console.WriteLine(string.Join(",", all));

Résultat:

1,2,3

Syntaxe

int[] values = [1, ..other, 5];

Exemple 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        int[] middle = [2, 3];
        int[] values = [1, ..middle, 4];
        Console.WriteLine(string.Join(",", values));
    }
}

Exemple 2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

public static class Program
{
    public static async Task Main(string[] args)
    {
        List<string> names = ["ore", "wood"];
        List<string> all = [..names, "food"];
        Console.WriteLine(all.Count);
    }
}

Source officielle : Microsoft Apprendre.

Clone this wiki locally