Enumerable.Append(IEnumerable<TSource>, TSource) Enumerable.Append(IEnumerable<TSource>, TSource) Enumerable.Append(IEnumerable<TSource>, TSource) Enumerable.Append(IEnumerable<TSource>, TSource) Method

定義

將值附加在序列結尾。Appends a value to the end of the sequence.

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Collections::Generic::IEnumerable<TSource> ^ Append(System::Collections::Generic::IEnumerable<TSource> ^ source, TSource element);
public static System.Collections.Generic.IEnumerable<TSource> Append<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource element);
static member Append : seq<'Source> * 'Source -> seq<'Source>
<Extension()>
Public Function Append(Of TSource) (source As IEnumerable(Of TSource), element As TSource) As IEnumerable(Of TSource)

類型參數

TSource

source 項目的類型。The type of the elements of source.

參數

source
IEnumerable<TSource>

一連串的值。A sequence of values.

element
TSource TSource TSource TSource

要附加在 source 後面的值。The value to append to source.

傳回

IEnumerable<TSource>

element 結尾的新序列。A new sequence that ends with element.

例外狀況

範例

下列程式碼範例示範如何使用Append來將值附加至序列的結尾。The following code example demonstrates how to use Append to append a value to the end of the sequence.

// Creating a list of numbers
List<int> numbers = new List<int> { 1, 2, 3, 4 };

// Trying to append any value of the same type
numbers.Append(5);

// It doesn't work because the original list has not been changed
Console.WriteLine(string.Join(", ", numbers));

// It works now because we are using a changed copy of the original list
Console.WriteLine(string.Join(", ", numbers.Append(5)));

// If you prefer, you can create a new list explicitly
List<int> newNumbers = numbers.Append(5).ToList();

// And then write to the console output
Console.WriteLine(string.Join(", ", newNumbers));

// This code produces the following output:
//
// 1, 2, 3, 4
// 1, 2, 3, 4, 5
// 1, 2, 3, 4, 5
' Creating a list of numbers
Dim numbers As New List(Of Integer)(New Integer() {1, 2, 3, 4})

' Trying to append any value of the same type
numbers.Append(5)

' It doesn't work because the original list has not been changed
Console.WriteLine(String.Join(", ", numbers))

' It works now because we are using a changed copy of the original list
Console.WriteLine(String.Join(", ", numbers.Append(5)))

' If you prefer, you can create a new list explicitly
Dim newNumbers As List(Of Integer) = numbers.Append(5).ToList

' And then write to the console output
Console.WriteLine(String.Join(", ", newNumbers))

' This code produces the following output:
'
' 1, 2, 3, 4
' 1, 2, 3, 4, 5
' 1, 2, 3, 4, 5

備註

注意

這個方法不會修改集合的元素。This method does not modify the elements of the collection. 相反地,它會建立集合的複本與新的項目。Instead, it creates a copy of the collection with the new element.

適用於