Επεξεργασία

Κοινή χρήση μέσω


LinkedList<T> Constructors

Definition

Initializes a new instance of the LinkedList<T> class.

Overloads

LinkedList<T>()

Initializes a new instance of the LinkedList<T> class that is empty.

LinkedList<T>(IEnumerable<T>)

Initializes a new instance of the LinkedList<T> class that contains elements copied from the specified IEnumerable and has sufficient capacity to accommodate the number of elements copied.

LinkedList<T>(SerializationInfo, StreamingContext)
Obsolete.

Initializes a new instance of the LinkedList<T> class that is serializable with the specified SerializationInfo and StreamingContext.

LinkedList<T>()

Source:
LinkedList.cs
Source:
LinkedList.cs
Source:
LinkedList.cs

Initializes a new instance of the LinkedList<T> class that is empty.

public:
 LinkedList();
public LinkedList ();
Public Sub New ()

Examples

The following code example creates and initializes a LinkedList<T> of type String, adds several nodes, and then displays its contents.

#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

Remarks

LinkedList<T> accepts null as a valid Value for reference types and allows duplicate values.

If the LinkedList<T> is empty, the First and Last properties contain null.

This constructor is an O(1) operation.

Applies to

LinkedList<T>(IEnumerable<T>)

Source:
LinkedList.cs
Source:
LinkedList.cs
Source:
LinkedList.cs

Initializes a new instance of the LinkedList<T> class that contains elements copied from the specified IEnumerable and has sufficient capacity to accommodate the number of elements copied.

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))

Parameters

collection
IEnumerable<T>

The IEnumerable whose elements are copied to the new LinkedList<T>.

Exceptions

collection is null.

Examples

For an example that includes this constructor, see the LinkedList<T> class.

Remarks

LinkedList<T> accepts null as a valid Value for reference types and allows duplicate values.

If collection has no elements then the new LinkedList<T> is empty, and the First and Last properties contain null.

This constructor is an O(n) operation, where n is the number of elements in collection.

Applies to

LinkedList<T>(SerializationInfo, StreamingContext)

Source:
LinkedList.cs
Source:
LinkedList.cs
Source:
LinkedList.cs

Caution

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

Initializes a new instance of the LinkedList<T> class that is serializable with the specified SerializationInfo and 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)

Parameters

info
SerializationInfo

A SerializationInfo object containing the information required to serialize the LinkedList<T>.

context
StreamingContext

A StreamingContext object containing the source and destination of the serialized stream associated with the LinkedList<T>.

Attributes

Remarks

LinkedList<T> accepts null as a valid Value for reference types and allows duplicate values.

If the LinkedList<T> is empty, the First and Last properties contain null.

This constructor is an O(n) operation.

See also

Applies to