LinkedList<T> 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 LinkedList<T> 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| LinkedList<T>() |
初始化一個空的類別新實例 LinkedList<T> 。 |
| LinkedList<T>(IEnumerable<T>) |
初始化一個包含從指定LinkedList<T>元素複製且容量足夠容納複製元素數量的類別的新實例IEnumerable。 |
| LinkedList<T>(SerializationInfo, StreamingContext) |
已淘汰.
初始化一個可序列化且指定的LinkedList<T>SerializationInfo類別新實例StreamingContext。 |
LinkedList<T>()
初始化一個空的類別新實例 LinkedList<T> 。
public:
LinkedList();
public LinkedList();
Public Sub New ()
範例
以下程式碼範例建立並初始化 一個 LinkedList<T> 型別 String為 的 ,新增多個節點,然後顯示其內容。
using System;
using System.Collections;
using System.Collections.Generic;
public class GenericCollection
{
public static void Main()
{
// Create and initialize a new LinkedList.
LinkedList<String> ll = new LinkedList<String>();
ll.AddLast("red");
ll.AddLast("orange");
ll.AddLast("yellow");
ll.AddLast("orange");
// Display the contents of the LinkedList.
if (ll.Count > 0)
{
Console.WriteLine("The first item in the list is {0}.", ll.First.Value);
Console.WriteLine("The last item in the list is {0}.", ll.Last.Value);
Console.WriteLine("The LinkedList contains:");
foreach (String s in ll)
Console.WriteLine(" {0}", s);
}
else
{
Console.WriteLine("The LinkedList is empty.");
}
}
}
/* This code produces the following output.
The first item in the list is red.
The last item in the list is orange.
The LinkedList contains:
red
orange
yellow
orange
*/
Imports System.Collections
Imports System.Collections.Generic
Public Class GenericCollection
Public Shared Sub Main()
' Create and initialize a new LinkedList.
Dim ll As New LinkedList(Of String)()
ll.AddLast("red")
ll.AddLast("orange")
ll.AddLast("yellow")
ll.AddLast("orange")
' Display the contents of the LinkedList.
If ll.Count > 0 Then
Console.WriteLine("The first item in the list is {0}.", ll.First.Value)
Console.WriteLine("The last item in the list is {0}.", ll.Last.Value)
Console.WriteLine("The LinkedList contains:")
For Each s As String In ll
Console.WriteLine(" {0}", s)
Next s
Else
Console.WriteLine("The LinkedList is empty.")
End If
End Sub
End Class
'This code produces the following output.
'
'The first item in the list is red.
'The last item in the list is orange.
'The LinkedList contains:
' red
' orange
' yellow
' orange
備註
LinkedList<T> 接受 null 為 的有效 Value 參考型別,並允許重複值。
若 為 LinkedList<T> 空,則 First 與 Last 屬性包含 null。
此構造子為 O(1) 運算。
適用於
LinkedList<T>(IEnumerable<T>)
初始化一個包含從指定LinkedList<T>元素複製且容量足夠容納複製元素數量的類別的新實例IEnumerable。
public:
LinkedList(System::Collections::Generic::IEnumerable<T> ^ collection);
public LinkedList(System.Collections.Generic.IEnumerable<T> collection);
new System.Collections.Generic.LinkedList<'T> : seq<'T> -> System.Collections.Generic.LinkedList<'T>
Public Sub New (collection As IEnumerable(Of T))
參數
- collection
- IEnumerable<T>
其 IEnumerable 元素會被複製到新的 LinkedList<T>。
例外狀況
collection 是 null。
範例
關於包含此建構子的範例,請參見 類別。LinkedList<T>
備註
LinkedList<T> 接受 null 為 的有效 Value 參考型別,並允許重複值。
若 collection 沒有元素,則 新 LinkedList<T> 為空,且 First 與 Last 性質包含 null。
此構造子為 O(n) 運算,其中 n 為 中的 collection元素數。
適用於
LinkedList<T>(SerializationInfo, StreamingContext)
警告
This API supports obsolete formatter-based serialization. It should not be called or extended by application code.
初始化一個可序列化且指定的LinkedList<T>SerializationInfo類別新實例StreamingContext。
protected:
LinkedList(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected LinkedList(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
protected LinkedList(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new System.Collections.Generic.LinkedList<'T> : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Generic.LinkedList<'T>
new System.Collections.Generic.LinkedList<'T> : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Generic.LinkedList<'T>
Protected Sub New (info As SerializationInfo, context As StreamingContext)
參數
- info
- SerializationInfo
一個 SerializationInfo 包含序列化所需 LinkedList<T>資訊的物件。
- context
- StreamingContext
一個 StreamingContext 包含與 LinkedList<T>相關聯序列化串流來源與目的地的物件。
- 屬性
備註
LinkedList<T> 接受 null 為 的有效 Value 參考型別,並允許重複值。
若 為 LinkedList<T> 空,則 First 與 Last 屬性包含 null。
此建構子為 O(n) 運算。