C# 排序集
按排序顺序排列的对象集合在 C# 中称为 SortedSet,该集合是通用类型,位于命名空间 Systems.Collections.Generic 命名空间下。 SortedSet 类还提供了数学中的集合操作,例如差、交和并,并且 SortedSet 的大小随着新元素向集合中的添加而增长,因此它是动态集合,可以查看、删除元素或添加到 SortedSet 中的集合中。SortedSet 按降序排序,SortedSet 中没有冗余的元素,这意味着只有唯一的元素存储在 SortedSet 中。
语法:
SortedSet<Type>set_name = new SortedSet<Type>();
其中 Type 是排序集的类型,
set_name 是集合的名称。
C# 中 SortedSet 的函数
- SortedSet 用于存储、查看和删除按降序排序的不同元素。
- SortedSet 中存储的元素类型必须相同。
- IReadOnlyCollection 接口、IDeserializationCallBack 接口、IEnumerable 接口、ISet 接口、ISerialized 接口均由 SortedSet 类实现。
- SortedSet 可以容纳的元素数量称为 SortedSet 的容量。
- SortedSet 中不能有重复元素,SortedSet 避免了冗余。
C# 中 SortedSet 的构造函数
以下是 C# SortedSet 的构造函数:
1。 SortedSet(): Sorted 类的新实例已初始化。
2。 SortedSet(IComparer):使用指定为参数的比较器初始化 Sorted 类的新实例。
3。 SortedSet(IEnumerable):初始化 Sorted 类的新实例,其中包含从指定为参数的可枚举集合中获取的元素。
4。 SortedSet(IEnumerable, IComparer):初始化 Sorted 类的新实例,其中包含从指定为参数的可枚举集合中获取的元素,并使用指定为参数的比较器。
5。 SortedSet(SerializationInfo, StreamingContext):初始化 Sorted 类的新实例,其中包含序列化的数据。
C# 中 SortedSet 的方法
以下是C# SortedSet的方法:
1。 Add(T):使用 Add(T) 方法将元素添加到 SortedSet,成功将元素添加到 SortedSet 后,会返回一个值,表示添加成功。
2。 UnionWith(IEnumerable): 当前 Sorted 对象发生更改,使其包含当前对象中存在的或指定为参数的集合中存在的所有元素。
3。 Clear(): SortedSet 的所有元素都被删除。
4。 TryGetValue(T,T): 在 SortedSet 中搜索指定为参数的值,如果找到该值,则返回相等的值。
5。包含(T):在排序集中检查指定为参数的元素,以查明它是否存在于排序集中。
6。 ToString(): 返回代表当前对象的字符串。
7。 CopyTo(): 将排序集中的部分元素或排序集中的所有元素复制到一个一维数组,该数组与排序集兼容,索引为数组的开头复制开始的位置或指定的索引。
8。 SymmetricExceptWith(IEnumerable): 当前 Sorted 对象已更改为仅包含当前对象中存在的元素或指定为参数的集合中存在的元素,但不包含两者中的元素。
9。 CreateSetComparer():使用 CreateSetComparer() 方法返回 IEqualityComparer 对象,使用该方法创建包含各个集合的集合。
10。 SetEquals(IEnumerable): SetEquals(IEnumerable) 方法检查排序集的当前对象和参数指定的集合中是否存在相同的元素。
11。 CreateSetComparer(IEqualityComparer):使用 CreateSetComparer(IEqualityComparer) 方法根据指定为参数的比较器返回 IEqualityComparer 对象,使用该方法创建包含各个集合的集合。
12。 Reverse():使用 Reverse() 方法返回 IEnumerable,该方法以相反的顺序循环遍历排序集。
13。 Equals(Object): 检查指定为参数的对象是否等于当前对象。
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)); } }
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.
以上是C# 排序集的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

使用 C# 的 Active Directory 指南。在这里,我们讨论 Active Directory 在 C# 中的介绍和工作原理以及语法和示例。

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。
