Home > Backend Development > C#.Net Tutorial > SortedSet class in C#

SortedSet class in C#

王林
Release: 2023-08-26 09:29:17
forward
1092 people have browsed it

C# 中的 SortedSet 类

The SortedSet class in C# represents a collection of objects that is maintained in sorted order.

Following are the properties of the SortedSet class −

##1234
Sr.No Property & Description
Comparer Gets the IComparer object that is used to order the values ​​in the SortedSet.

CountGets the number of elements in the SortedSet.

MaxGets the maximum value in the SortedSet, as defined by the comparer.

MinGets the minimum value in the SortedSet< ;T>, as Defined by the comparator.

The following are some methods of the SortedSet class:

Serial numberMethod and Description##1Add(T) indicates if it was successfully added.
Add elements to the collection , and returns a value indicating whether the element was successfully added.

2

##Clear() Removes all elements from the set.

##3

Contains(T) Determines whether the set contains a specific element.

##4

CopyTo(T[]) Copies the complete SortedSet to a compatible onedimensional array, starting at the beginning of the target array.

5

CopyTo(T[], Int32) Copies the complete SortedSet to a compatible onedimensional array, starting at the specified array index.

##6

CopyTo(T[ ], Int32, Int32) Copies a specified number of elements Convert from SortedSet to compatible 1D array array, starting at the specified array index. ##7

CreateSetComparer()

Returns an IEqualityComparer object that can be used to Create a collection that contains individual collections.

Examples

Now let’s see some examples −

To check if a SortedSet contains a specific element, the code As follows − Real-time demonstration

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      SortedSet<string> set1 = new SortedSet<string>();
      set1.Add("CD");
      set1.Add("CD");
      set1.Add("CD");
      set1.Add("CD");
      Console.WriteLine("Elements in SortedSet1...");
      foreach (string res in set1) {
         Console.WriteLine(res);
      }
      Console.WriteLine("Does the SortedSet1 contains the element DE? = "+set1.Contains("DE"));
      SortedSet<string> set2 = new SortedSet<string>();
      set2.Add("BC");
      set2.Add("CD");
      set2.Add("DE");
      set2.Add("EF");
      set2.Add("AB");
      set2.Add("HI");
      set2.Add("JK");
      Console.WriteLine("Elements in SortedSet2...");
      foreach (string res in set2) {
         Console.WriteLine(res);
      }
      Console.WriteLine("SortedSet2 is a superset of SortedSet1? = "+set2.IsSupersetOf(set1));
   }
}
Copy after login

Output

This will produce the following output −

Elements in SortedSet1...
CD
Does the SortedSet1 contains the element DE? = False
Elements in SortedSet2...
AB
BC
CD
DE
EF
HI
JK
SortedSet2 is a superset of SortedSet1? = True
Copy after login

To obtain an enumerator that traverses SortedSet, the code is as follows −

Example

Online Demonstration

using System;
using System.Collections.Generic;
public class Demo {
   public static void Main(){
      SortedSet<string> set1 = new SortedSet<string>();
      set1.Add("AB");
      set1.Add("BC");
      set1.Add("CD");
      set1.Add("EF");
      Console.WriteLine("Elements in SortedSet1...");
      foreach (string res in set1) {
         Console.WriteLine(res);
      }
      SortedSet<string> set2 = new SortedSet<string>();
      set2.Add("BC");
      set2.Add("CD");
      set2.Add("DE");
      set2.Add("EF");
      set2.Add("AB");
      set2.Add("HI");
      set2.Add("JK");
      Console.WriteLine("Elements in SortedSet2 (Enumerator for SortedSet)...");
      SortedSet<string>.Enumerator demoEnum = set2.GetEnumerator();
      while (demoEnum.MoveNext()) {
         string res = demoEnum.Current;
         Console.WriteLine(res);
      }
   }
}
Copy after login

Output

This will produce the following output −

Elements in SortedSet1...
AB
BC
CD
EF
Elements in SortedSet2 (Enumerator for SortedSet)...
AB
BC
CD
DE
EF
HI
JK
Copy after login

The above is the detailed content of SortedSet class in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template