美文网首页
C# 中的SortedSet

C# 中的SortedSet

作者: 寻找无名的特质 | 来源:发表于2021-02-07 06:04 被阅读0次

Set是包含不重复对象的集合,可以用来对两个集合求交集、并集、差集等运算。SortedSet中的元素可以按照一定规则进行排序,这样可以按照某种顺序遍历元素。这需要创建针对某种类型排序的类,这个类需要实现IComparer接口。比如,我们需要构造类型StateConfigure的SortedSet,就需要有针对StateConfigure的IComparer类,如下:

namespace ZL.FSM.Core.STD
{
    class StateConfigureCompare : IComparer<StateConfigure>
    {
        public int Compare(StateConfigure x, StateConfigure y)
        {
            return x.DocumentOrder-y.DocumentOrder;
        }
    }
}

初始化SortedSet<StateConfigure>的代码如下:

 var statesToEnter = new SortedSet<StateConfigure>(new StateConfigureCompare());

相关文章

网友评论

      本文标题:C# 中的SortedSet

      本文链接:https://www.haomeiwen.com/subject/igystltx.html