C# 运算符重载?

C# 运算符重载?,c#,operators,C#,Operators,我自己制作了一个rss阅读器,让我随时了解最新情况,并在新的节目中通知我,至少这是我的想法 我已经制作了一个结构“季节情节”,它包含两个整数(季节+情节)和一个覆盖ToString函数 我存储本地观看的最新内容,然后从rss中阅读最新内容。但是我怎么能比较季节性事件呢?现在我把每一个整数都拿来比较 if( se1.Season >= se2.Season ) if( se1.Episode > se2.Episode || se1.Season > se2.Season

我自己制作了一个rss阅读器,让我随时了解最新情况,并在新的节目中通知我,至少这是我的想法

我已经制作了一个结构“季节情节”,它包含两个整数(季节+情节)和一个覆盖ToString函数

我存储本地观看的最新内容,然后从rss中阅读最新内容。但是我怎么能比较季节性事件呢?现在我把每一个整数都拿来比较

if( se1.Season >= se2.Season )
    if( se1.Episode > se2.Episode || se1.Season > se2.Season )
        // new episode!
我真正想要的是

if( se1 > se2 )
    // new episode
我能得到任何帮助吗?

有两种方法:

  • 实施和使用
    比较
  • 我建议您使用两种方式:

    public class SeasonEpisode : IComparable<SeasonEpisode>
    {
        public int CompareTo(SeasonEpisode other)
        {
            if(other == null)
                return 1;
            if(Season == other.Season)
            {
                if(Episode == other.Episode)
                    return 0;
                else if(Episode < other.Episode)
                    return -1;
                else
                    return 1;
            }
            else if(Season < other.Season) 
                return -1;
            else
                return 1;
        }
    
        public static bool operator <(SeasonEpisode e1, SeasonEpisode e2) 
        {
            return e1.CompareTo(e2) < 0;
        }
    
        public static bool operator >(SeasonEpisode e1, SeasonEpisode e2) 
        {
            return e1.CompareTo(e2) > 0;
        }
    }
    
    public类季节性插曲:IComparable
    {
    公共int比较(季节性插曲其他)
    {
    如果(其他==null)
    返回1;
    如果(季节==其他季节)
    {
    如果(插曲==其他.插曲)
    返回0;
    else if(插曲<其他插曲)
    返回-1;
    其他的
    返回1;
    }
    else if(季节<其他季节)
    返回-1;
    其他的
    返回1;
    }
    公共静态布尔运算符(第1季、第2季)
    {
    返回e1。比较到(e2)>0;
    }
    }
    
    您可以实现该接口

    定义类型实现用于比较两个对象的方法

    如果希望一个类与该类的另一个实例具有可比性,则可以实现
    IComparable
    。这可能就是你想要的,在这种情况下


    如果您需要一个比较两个对象的类,请实现
    IComparer

    当我遇到
    NullReferenceException
    时,这里有一个改进(这可能是主观的;-))

    唯一的变化是,如果
    null
    s:/p 公开课前的季节插曲:Icomparableseasonepicode { 专用静态整数比较(第1季、第2季) { if(e1==null&&e2==null) 返回0; else if(e1==null) 返回-1; else if(e2==null) 返回1; 如果(e1.季节==e2.季节) { 如果(e1.插曲==e2.插曲) 返回0; else if(e1.插曲0; } }
    (针对您答案的旧版本,您答案的编辑并没有改变我评论的概念。懒得重述…)这里使用的界面并不正确。您可能需要一个实现此接口的比较器类,或者在
    中实现它,并像这样调用它:
    se1.Compare(se1,se2)
    。有点偏执<代码>IComparable
    是您在此处要使用的内容。另请参见:如果
    e1
    null
    @BatteryBackupUnit,则运算符将抛出
    null引用异常。谢谢你的帮助。
    
    public class SeasonEpisode : IComparable<SeasonEpisode>
    {
        private static int Compare(SeasonEpisode e1, SeasonEpisode e2)
        {
            if (e1 == null && e2 == null)
                return 0;
            else if (e1 == null)
                return -1;
            else if (e2 == null)
                return 1;
    
            if(e1.Season == e2.Season)
            {
                if(e1.Episode == e2.Episode)
                    return 0;
                else if(e1.Episode < e2.Episode)
                    return -1;
                else
                    return 1;
            }
            else if(e1.Season < e2.Season) 
                return -1;
            else
                return 1;
        }    
    
        public int CompareTo(SeasonEpisode other)
        {
            return Compare(this, other);
        }
    
        public static bool operator <(SeasonEpisode e1, SeasonEpisode e2) 
        {
            return Compare(e1, e2) < 0;
        }
    
        public static bool operator >(SeasonEpisode e1, SeasonEpisode e2) 
        {
            return Compare(e1, e2) > 0;
        }
    }