Heim > Artikel > Backend-Entwicklung > C# SortedSet
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>();
Where Type is the type of the sorted set and
set_name is the name of the set.
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.
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.
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)); } }
Output:
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!