Enumerable.Append<TSource>(IEnumerable<TSource>, TSource) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将一个值追加到序列末尾。
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
的元素类型。
参数
- source
- IEnumerable<TSource>
值序列。
- element
- TSource
要追加到 source
的值。
返回
IEnumerable<TSource>
以 element
结尾的新序列。
例外
source
为 null
。
示例
下面的代码示例演示如何使用 Append 将值追加到序列的末尾。
// 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
注解
注意
此方法不会修改集合的元素。 而是使用新元素创建集合的副本。