Skip to content

JA CSharp Operator Index Range

Codex edited this page Oct 11, 2026 · 2 revisions

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

終了インデックスと範囲

ディレクトリ C# · 実践的な指導

^1 は最後の要素を示します。範囲には右側の境界線は含まれません。 .. unbounded は、サポートされているオブジェクト全体を意味します。

簡単な例 (Example.cs)

この例を Example.cs として別に保存し、このページの他の例とは独立して実行してください。

Example.cs:

// PROJECT-EXAMPLE
int[] values = [10, 20, 30];
System.Console.WriteLine(values[^1]);
System.Console.WriteLine(string.Join(",", values[..2]));

結果:

30
10,20

構文

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

例 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]));
    }
}

例 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..]);
    }
}

公式ソース: Microsoft Learn。

Clone this wiki locally