Repository navigation
ZH TW CSharp Operator Collection Expression
Codex edited this page Oct 11, 2026
·
2 revisions
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
集合表達式需要目標類型; spread 新增另一個序列的元素。不要將傳播範圍與範圍混淆。
請將此範例另存為 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];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));
}
}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學習。
ClassicUO — Bad Newbie & C# Scripts IDE