SortedSet<T> Constructors

Definition

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

Overloads

SortedSet<T>()

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

SortedSet<T>(IComparer<T>)

Initializes a new instance of the SortedSet<T> class that uses a specified comparer.

SortedSet<T>(IEnumerable<T>)

Initializes a new instance of the SortedSet<T> class that contains elements copied from a specified enumerable collection.

SortedSet<T>(IEnumerable<T>, IComparer<T>)

Initializes a new instance of the SortedSet<T> class that contains elements copied from a specified enumerable collection and that uses a specified comparer.

SortedSet<T>(SerializationInfo, StreamingContext)
Obsolete.

Initializes a new instance of the SortedSet<T> class that contains serialized data.

Remarks

This constructor is an O(1) operation.

SortedSet<T>()

Source:
SortedSet.cs
Source:
SortedSet.cs
Source:
SortedSet.cs

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

C#
public SortedSet();

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

SortedSet<T>(IComparer<T>)

Source:
SortedSet.cs
Source:
SortedSet.cs
Source:
SortedSet.cs

Initializes a new instance of the SortedSet<T> class that uses a specified comparer.

C#
public SortedSet(System.Collections.Generic.IComparer<T> comparer);
C#
public SortedSet(System.Collections.Generic.IComparer<T>? comparer);

Parameters

comparer
IComparer<T>

The default comparer to use for comparing objects.

Exceptions

comparer is null.

Examples

The following example defines a comparer (ByFileExtension) that is used to construct a sorted set that sorts file names by their extensions. This code example is part of a larger example provided for the SortedSet<T> class.

C#
// Create a sorted set using the ByFileExtension comparer.
var mediaFiles1 = new SortedSet<string>(new ByFileExtension());
C#
// Defines a comparer to create a sorted set
// that is sorted by the file extensions.
public class ByFileExtension : IComparer<string>
{
    string xExt, yExt;

    CaseInsensitiveComparer caseiComp = new CaseInsensitiveComparer();

    public int Compare(string x, string y)
    {
        // Parse the extension from the file name.
        xExt = x.Substring(x.LastIndexOf(".") + 1);
        yExt = y.Substring(y.LastIndexOf(".") + 1);

        // Compare the file extensions.
        int vExt = caseiComp.Compare(xExt, yExt);
        if (vExt != 0)
        {
            return vExt;
        }
        else
        {
            // The extension is the same,
            // so compare the filenames.
            return caseiComp.Compare(x, y);
        }
    }
}

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

SortedSet<T>(IEnumerable<T>)

Source:
SortedSet.cs
Source:
SortedSet.cs
Source:
SortedSet.cs

Initializes a new instance of the SortedSet<T> class that contains elements copied from a specified enumerable collection.

C#
public SortedSet(System.Collections.Generic.IEnumerable<T> collection);

Parameters

collection
IEnumerable<T>

The enumerable collection to be copied.

Remarks

Duplicate elements in the enumerable collection are not copied into the new instance of the SortedSet<T> class, and no exceptions are thrown.

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

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

SortedSet<T>(IEnumerable<T>, IComparer<T>)

Source:
SortedSet.cs
Source:
SortedSet.cs
Source:
SortedSet.cs

Initializes a new instance of the SortedSet<T> class that contains elements copied from a specified enumerable collection and that uses a specified comparer.

C#
public SortedSet(System.Collections.Generic.IEnumerable<T> collection, System.Collections.Generic.IComparer<T> comparer);
C#
public SortedSet(System.Collections.Generic.IEnumerable<T> collection, System.Collections.Generic.IComparer<T>? comparer);

Parameters

collection
IEnumerable<T>

The enumerable collection to be copied.

comparer
IComparer<T>

The default comparer to use for comparing objects.

Exceptions

collection is null.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

SortedSet<T>(SerializationInfo, StreamingContext)

Source:
SortedSet.cs
Source:
SortedSet.cs
Source:
SortedSet.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 SortedSet<T> class that contains serialized data.

C#
[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 SortedSet(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
C#
protected SortedSet(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);

Parameters

info
SerializationInfo

The object that contains the information that is required to serialize the SortedSet<T> object.

context
StreamingContext

The structure that contains the source and destination of the serialized stream associated with the SortedSet<T> object.

Attributes

Remarks

This constructor is called during deserialization to reconstitute an object that is transmitted over a stream.

Applies to

.NET 10 and other versions
Product Versions (Obsolete)
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 (8, 9, 10)
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1