Skip to content

JA CSharp Operator Collection Expression

Codex edited this page Oct 11, 2026 · 2 revisions

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

コレクションと普及

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

コレクション式にはターゲット型が必要です。スプレッドは別のシーケンスの要素を追加します。スプレッドと範囲を混同しないでください。

簡単な例 (Example.cs)

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

Example.cs:

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

結果:

1,2,3

構文

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

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

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

公式ソース: Microsoft Learn。

Clone this wiki locally