Skip to content

ES CSharp Operator Collection Expression

Codex edited this page Oct 11, 2026 · 2 revisions

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

Colecciones y difusión

Manual C# · Guía Práctica

La expresión de colección requiere un tipo de destino; spread agrega elementos de otra secuencia. No confunda extensión con alcance.

Ejemplo sencillo (Example.cs)

Guarde este ejemplo por separado como Example.cs. Ejecútelo independientemente de los demás ejemplos de esta página.

Example.cs:

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

Resultado:

1,2,3

Sintaxis

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

Ejemplo 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));
    }
}

Ejemplo 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);
    }
}

Fuente oficial: Microsoft Aprender.

Clone this wiki locally