LinkedList<T> 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 LinkedList<T> 類別的新執行個體。
多載
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 ()
範例
下列程式代碼範例會建立並初始化 類型的 String,並新增數個LinkedList<T>節點,然後顯示其內容。
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
void main()
{
// Create and initialize a new LinkedList.
LinkedList< String^ > ^ ll = gcnew LinkedList< String^ >;
ll->AddLast(L"red");
ll->AddLast(L"orange");
ll->AddLast(L"yellow");
ll->AddLast(L"orange");
// Display the contents of the LinkedList.
if (ll->Count > 0)
{
Console::WriteLine(L"The first item in the list is {0}.", ll->First->Value);
Console::WriteLine(L"The last item in the list is {0}.", ll->Last->Value);
Console::WriteLine(L"The LinkedList contains:");
for each (String^ s in ll)
{
Console::WriteLine(L" {0}", s);
}
}
else
{
Console::WriteLine(L"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
*/
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);
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);
new System.Collections.Generic.LinkedList<'T> : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Collections.Generic.LinkedList<'T>
[<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>
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
。
此建構函式是 n 個) 作業的 O (。