Skip to content

ES CSharp Operator Index Range

Codex edited this page Oct 11, 2026 · 2 revisions

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

Índices y rangos finales

Manual C# · Guía Práctica

^1 denota el último elemento. El rango excluye el borde derecho; .. ilimitado significa todo el objeto admitido.

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[] values = [10, 20, 30];
System.Console.WriteLine(values[^1]);
System.Console.WriteLine(string.Join(",", values[..2]));

Resultado:

30
10,20

Sintaxis

items[^1] / items[start..end]

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[] values = [10, 20, 30, 40];
        Console.WriteLine(values[^1]);
        Console.WriteLine(string.Join(",", values[1..3]));
    }
}

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)
    {
        string name = "archer";
        Console.WriteLine(name[..3]);
        Console.WriteLine(name[^2..]);
    }
}

Fuente oficial: Microsoft Aprender.

Clone this wiki locally