Inhaltsverzeichnis
Functions of SortedSet in C#
Constructors of SortedSet in C#
Methods of SortedSet in C#
Example

C# SortedSet

Sep 03, 2024 pm 03:24 PM
c# c# tutorial

The collection of objects in sorted order is called SortedSet in C# and the collection is of the generic type which comes under the namespace Systems.Collections.Generic namespace. And the set operations of mathematics like difference, intersection, and union is also provided by SortedSet class and the size of the SortedSet grows with the addition of new elements to the set and hence it is the dynamic collection and the elements can be viewed, removed or added to the collection in SortedSet.The SortedSet is sorted in the decreasing order with no redundancy of elements in the SortedSet meaning only unique elements are stored in the SortedSet.

Syntax:

SortedSet<Type>set_name = new SortedSet<Type>();
Nach dem Login kopieren

Where Type is the type of the sorted set and

set_name is the name of the set.

Functions of SortedSet in C#

  • SortedSet is used to store, view, and remove the distinct elements which are sorted in decreasing order.
  • The type of elements to be stored in the SortedSet must be the same.
  • IReadOnlyCollection interface, IDeserializationCallBack interface, IEnumerable interface, ISet interface, ISerializable interfaces are implemented by SortedSet class.
  • The number of elements that can be held by the SortedSet is called the capacity of the SortedSet.
  • There cannot be any duplicate elements in the SortedSet and the SortedSet avoids redundancy.

Constructors of SortedSet in C#

Below are the Constructors of C# SortedSet:

1. SortedSet(): A new instance of the Sorted class is initialized.

2. SortedSet(IComparer): A new instance of the Sorted class is initialized which uses the Comparer specified as the parameter.

3. SortedSet(IEnumerable): A new instance of the Sorted class is initialized which consists of elements that are taken from the collection of enumerable specified as the parameter.

4. SortedSet(IEnumerable, IComparer): A new instance of the Sorted class is initialized which consists of elements that are taken from the collection of enumerable specified as the parameter and uses a comparer which is specified as a parameter.

5. SortedSet(SerializationInfo, StreamingContext): A new instance of the Sorted class is initialized which consists of data that is serialized.

Methods of SortedSet in C#

Below are the methods of C# SortedSet:

1. Add(T): An element is added to the SortedSet using Add(T) method and upon successful addition of an element to the SortedSet, a value is returned indicating the successful addition.

2. UnionWith(IEnumerable): The current Sorted object is changed in such a way that it consists of all the elements present in the current object or present in the collection specified as a parameter.

3. Clear(): All the elements of the SortedSet are removed.

4. TryGetValue(T,T): The SortedSet is searched for the value specified as a parameter and if the value is found, equal value is returned.

5. Contains(T): An element specified as the parameter is checked for in the Sorted Set to find out if it is present in the Sorted set or not.

6. ToString(): A string is returned which represents the current object.

7. CopyTo(): Either some of the elements in the sorted set or all the elements in the sorted set are copied to an array which is one dimensional and is compatible with the sorted set and the index being the beginning of the array from where the copying starts or the index that is specified.

8. SymmetricExceptWith(IEnumerable): The current Sorted object is changed in such a way that it consists only the elements present in the current object or present in the collection specified as a parameter but not in both.

9. CreateSetComparer(): An IEqualityComparer object is returned by using CreateSetComparer() method using which a collection is created containing individual sets.

10. SetEquals(IEnumerable): The SetEquals(IEnumerable) method checks if the same elements are present in the current object of the sorted set and the collection specified as the parameter.

11. CreateSetComparer(IEqualityComparer): An IEqualityComparer object is returned as per the comparer specified as the parameter by using CreateSetComparer(IEqualityComparer) method using which a collection is created containing individual sets.

12. Reverse(): An IEnumerable is returned by using the Reverse() method which loops over the sorted set in a reversing order.

13. Equals(Object): The object specified as a parameter is checked to see if it is equal to the current object or not.

14. RemoveWhere(Predicate): All the elements of the sorted set matching the conditions set by the predicate specified as a parameter is removed.

15. ExceptWith(IEnumerable): The elements in the collection specified as the parameter are removed from the current sorted set object.

16. Remove(T): The item specified as the parameter will be removed from the sorted set.

17. GetEnumerator(): An Enumerator is returned using GetEnumertor() method which loops through the sorted set.

18. Overlaps(IEnumerable): The Overlaps(IEnumerable) method is used to check if the elements in the current sorted set and the elements in the collection specified as parameters are the same.

19. GetHashCode(): The GetHashCode() method is the hash function by default.

20. OnDeserilaization(Object): The event of deserialization is raised after the completion of deserialization and the ISerializable interface is implemented.

21. GetObjectData(SerilaizationInfo, StreamingContext): The data that is necessary to serialize a sorted set object is returned, and the ISerializableinterface is implemented.

22. MemberwiseClone(): The shallow copy of the current object is created.

23. GetType(): The type of the current instance is returned.

24. IsSupersetOf(IEnumerable): The IsSupersetOf(IEnumerable) method is used to determine if the object of a sorted set is a superset of the collection specified as a parameter.

25. GetViewBetween(T,T): A view of the subset in the sorted set is returned.

26. IsSubsetOf(IEnumerable): The IsSubsetOf(IEnumerable) method is used to determine if the object of a sorted set is a subset of the collection specified as a parameter.

27. IntersectWith(IEnumerable)The current Sorted object is changed in such a way that it consists only of the elements present in the collection specified as a parameter.

28. IsProperSupersetOf(IEnumerable): The IsProperSupersetOf(IEnumerable) method is used to determine if the object of the sorted set is a proper superset of the collection specified as a parameter.

29. IsProperSubsetOf(IEnumerable): The IsProperSubsetOf(IEnumerable) method is used to determine if the object of a sorted set is a proper subset of the collection specified as a parameter.

Example

Below are the examples of C# SortedSet:

C# program to create a SortedSetby using Add(T) method and then demonstrate the Remove(T) method and IsSubsetof(IEnumerable) method.

Code:

using System;
using System.Collections.Generic;
class program
{
public static void Main()
{
SortedSet<string>Set = new SortedSet<string>();
Set.Add("Shobha");
Set.Add("Ramya");
Set.Add("Nandan");
Set.Add("Nalina");
Set.Add("Sindhu");
Console.WriteLine("The elements of the sorted set are:");
foreach(string t in Set)
{
Console.WriteLine(t);
}
Console.WriteLine("The elements of the sorted set after using Remove method are:");
Set.Remove("Sindhu");
Set.Remove("Nalina");
foreach(string x in Set)
{
Console.WriteLine(x);
}
SortedSet<string> Set1 = new SortedSet<string>();
Set1.Add("Sahana");
Set1.Add("Suhaas");
Set1.Add("Nalina");
Console.WriteLine("Checking if the elements of the sorted set is a subset of the first set:");
Console.WriteLine(Set1.IsSubsetOf(Set));
}
}
Nach dem Login kopieren

Output:

C# SortedSet

Explanation: In the above program, a class called program is called. Then the main method is called. Then a sorted set to store the strings is created. Then elements are added to the sorted set using add() method. Then the foreach loop is used to display the elements of the sorted set. Then remove() method is used to remove the elements of the sorted set. Then again the foreach loop is used to display the elements of the sorted set. Then again a new sorted set to store strings is created. Then again elements are added to the new sorted set using add() method. Then IsSubsetof() method is used to check if the newly created sorted set is a subset of the first sorted set. The output of the program is shown in the snapshot above.

Das obige ist der detaillierte Inhalt vonC# SortedSet. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

Java-Tutorial
1662
14
PHP-Tutorial
1261
29
C#-Tutorial
1234
24
Active Directory mit C# Active Directory mit C# Sep 03, 2024 pm 03:33 PM

Leitfaden zu Active Directory mit C#. Hier besprechen wir die Einführung und die Funktionsweise von Active Directory in C# sowie die Syntax und das Beispiel.

C#-Serialisierung C#-Serialisierung Sep 03, 2024 pm 03:30 PM

Leitfaden zur C#-Serialisierung. Hier besprechen wir die Einführung, die Schritte des C#-Serialisierungsobjekts, die Funktionsweise bzw. das Beispiel.

Zufallszahlengenerator in C# Zufallszahlengenerator in C# Sep 03, 2024 pm 03:34 PM

Leitfaden zum Zufallszahlengenerator in C#. Hier besprechen wir die Funktionsweise des Zufallszahlengenerators, das Konzept von Pseudozufallszahlen und sicheren Zahlen.

C#-Datenrasteransicht C#-Datenrasteransicht Sep 03, 2024 pm 03:32 PM

Leitfaden zur C#-Datenrasteransicht. Hier diskutieren wir die Beispiele, wie eine Datenrasteransicht aus der SQL-Datenbank oder einer Excel-Datei geladen und exportiert werden kann.

Fakultät in C# Fakultät in C# Sep 03, 2024 pm 03:34 PM

Leitfaden zur Fakultät in C#. Hier diskutieren wir die Einführung in die Fakultät in C# zusammen mit verschiedenen Beispielen und Code-Implementierungen.

Der Unterschied zwischen Multithreading und asynchronem C# Der Unterschied zwischen Multithreading und asynchronem C# Apr 03, 2025 pm 02:57 PM

Der Unterschied zwischen Multithreading und Asynchron besteht darin, dass Multithreading gleichzeitig mehrere Threads ausführt, während asynchron Operationen ausführt, ohne den aktuellen Thread zu blockieren. Multithreading wird für rechenintensive Aufgaben verwendet, während asynchron für die Benutzerinteraktion verwendet wird. Der Vorteil des Multi-Threading besteht darin, die Rechenleistung zu verbessern, während der Vorteil von Asynchron nicht darin besteht, UI-Threads zu blockieren. Die Auswahl von Multithreading oder Asynchron ist von der Art der Aufgabe abhängt: Berechnungsintensive Aufgaben verwenden Multithreading, Aufgaben, die mit externen Ressourcen interagieren und die UI-Reaktionsfähigkeit asynchron verwenden müssen.

Muster in C# Muster in C# Sep 03, 2024 pm 03:33 PM

Leitfaden zu Mustern in C#. Hier besprechen wir die Einführung und die drei wichtigsten Arten von Mustern in C# zusammen mit ihren Beispielen und der Code-Implementierung.

Primzahlen in C# Primzahlen in C# Sep 03, 2024 pm 03:35 PM

Leitfaden zu Primzahlen in C#. Hier besprechen wir die Einführung und Beispiele von Primzahlen in C# sowie die Codeimplementierung.

See all articles