至少有一个对象必须实现IComparable

use*_*824 37 c#

using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedSet<Player> PlayerList = new SortedSet<Player>();

            while (true)
            {
                string Input;
                Console.WriteLine("What would you like to do?");
                Console.WriteLine("1. Create new player and score.");
                Console.WriteLine("2. Display Highscores.");
                Console.WriteLine("3. Write out to XML file.");
                Console.Write("Input Number: ");
                Input = Console.ReadLine();
                if (Input == "1")
                {
                    Player player = new Player();
                    string PlayerName;
                    string Score;

                    Console.WriteLine();
                    Console.WriteLine("-=CREATE NEW PLAYER=-");
                    Console.Write("Player name: ");
                    PlayerName = Console.ReadLine();
                    Console.Write("Player score: ");
                    Score = Console.ReadLine();

                    player.Name = PlayerName;
                    player.Score = Convert.ToInt32(Score);

                    //====================================
                    //ERROR OCCURS HERE
                    //====================================
                    PlayerList.Add(player);


                    Console.WriteLine("Player \"" + player.Name + "\" with the score of \"" + player.Score + "\" has been created successfully!" );
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("INVALID INPUT");
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我一直得到"

至少有一个对象必须实现IComparable.

"当试图添加第二个玩家时,第一个玩家会工作,但第二个玩家不会.我也必须使用,SortedSet因为这是工作的要求,这是学校的工作.

Jon*_*eet 66

好吧,你正试图使用SortedSet<>......这意味着你关心订购.但是通过它的声音你的Player类型没有实现IComparable<Player>.那么你期望看到什么样的订单?

基本上,您需要告诉您的Player代码如何比较一个玩家与另一个玩家.或者,您可以在IComparer<Player>其他地方实现,并将该比较传递给构造函数,SortedSet<>以指示您希望玩家进入的顺序.例如,您可以:

public class PlayerNameComparer : IComparer<Player>
{
    public int Compare(Player x, Player y)
    {
        // TODO: Handle x or y being null, or them not having names
        return x.Name.CompareTo(y.Name);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后:

// Note name change to follow conventions, and also to remove the
// implication that it's a list when it's actually a set...
SortedSet<Player> players = new SortedSet<Player>(new PlayerNameComparer());
Run Code Online (Sandbox Code Playgroud)

  • +1,因为这是实际解释OP**的唯一答案,为什么**他需要实现IComparable (12认同)
  • @d453:执行的每个成对比较中至少有一个对象。实际上,只有*某些*元素具有可比性是非常奇怪的。 (2认同)

Ali*_*ian 18

有时当您忘记 order 属性时它会引发

SortedSet<Player> players = players.OrderBy(c => c);
Run Code Online (Sandbox Code Playgroud)

但它必须是这样的:

SortedSet<Player> players = players.OrderBy(c => c.PlayerName);
Run Code Online (Sandbox Code Playgroud)

  • 这是我看到的与此错误相关的最常见错误。 (2认同)

小智 5

我想这是对这个错误的更一般的答案。

此行将失败并出现错误:

Items.OrderByDescending(t => t.PointXYZ);
Run Code Online (Sandbox Code Playgroud)

但是您可以指定如何直接比较它:

Items.OrderByDescending(t => t.PointXYZ.DistanceTo(SomeOtherPoint))
Run Code Online (Sandbox Code Playgroud)

那么您就不需要 IComparable 接口。取决于您使用的 API。就我而言,我有一个 Point 和 DistanceTo 方法。(Revit API)但是整数应该更容易确定其“大小/位置”。