📜  C#SortedSet(1)

📅  最后修改于: 2023-12-03 15:14:27.495000             🧑  作者: Mango

C# SortedSet

The C# SortedSet class is a collection class in the .NET framework that represents a sorted set of elements that may not contain duplicate entries. It is implemented as a binary search tree and provides efficient searching, inserting, and deleting operations.

Declaration

The SortedSet class is declared in the System.Collections.Generic namespace as follows:

public class SortedSet<T> : ICollection<T>, IEnumerable<T>, IEnumerable, IReadOnlyCollection<T>

Here, T is the type of elements in the set.

Constructors

The SortedSet class provides several constructors, allowing you to create a new sorted set:

public SortedSet();
public SortedSet(IEnumerable<T> collection);
public SortedSet(IComparer<T> comparer);
public SortedSet(IEnumerable<T> collection, IComparer<T> comparer);
public SortedSet(SerializationInfo info, StreamingContext context);
Properties

The SortedSet class provides the following properties:

  • Count: Gets the number of elements in the set.
  • Comparer: Gets the comparer used to compare elements in the set.
  • Max: Gets the maximum value in the set.
  • Min: Gets the minimum value in the set.
Methods

The SortedSet class provides the following methods:

  • Add(T item): Adds an element to the set. Returns a Boolean value indicating whether the element was added successfully.
  • Clear(): Removes all elements from the set.
  • Contains(T item): Determines whether the set contains a specific element.
  • CopyTo(T[] array, int arrayIndex): Copies the elements of the set to an array.
  • ExceptWith(IEnumerable other): Removes all elements in another collection from the set.
  • GetEnumerator(): Returns an enumerator that iterates through the set.
  • IntersectWith(IEnumerable other): Removes all elements from the set except those that are also in another collection.
  • IsProperSubsetOf(IEnumerable other): Determines whether the set is a proper subset of another collection.
  • IsProperSupersetOf(IEnumerable other): Determines whether the set is a proper superset of another collection.
  • IsSubsetOf(IEnumerable other): Determines whether the set is a subset of another collection.
  • IsSupersetOf(IEnumerable other): Determines whether the set is a superset of another collection.
  • Overlaps(IEnumerable other): Determines whether the set overlaps with another collection.
  • Remove(T item): Removes a specific element from the set.
  • SetEquals(IEnumerable other): Determines whether the set is equal to another collection.
  • SymmetricExceptWith(IEnumerable other): Modifies the set to contain only elements that are present in the set or in another collection, but not both.
  • UnionWith(IEnumerable other): Adds all elements from another collection to the set.
Example
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        SortedSet<string> set = new SortedSet<string>(new[]{"apple", "banana", "orange"});

        set.Add("pear");
        foreach (string fruit in set)
        {
            Console.WriteLine(fruit);
        }
    }
}

Output:

apple
banana
orange
pear

In this example, we created a new sorted set of strings that contains "apple", "banana", and "orange". We then added "pear" to the set and displayed the set's contents using a foreach loop. Since the elements in the set are sorted, the output is in alphabetical order.

Conclusion

The C# SortedSet class provides an efficient way to maintain a collection of sorted, unique elements. It provides a range of operations to manipulate the set and is a valuable addition to the .NET framework's collection classes.