无法将类型“string”隐式转换为“XXX.ClassName”

我创建了一个名为 Student 的类,Program.cs 中有以下代码:


public static IList<Student> Students { get; private set; }


private static void AddStudent()

    {

        /*try

        {

            Console.Write("First name: ");

            //Students.FirstName = Console.ReadLine();

            string firstName = Console.ReadLine(); 

        }

        catch (ArgumentNullException)

        {

            Console.WriteLine("Name was left empty.");

        }*/


        Console.Write("First name: ");

        string firstName = Console.ReadLine();


        Console.Write("Last name: ");

        string lastName = Console.ReadLine();            


        var newStudent = new Student {FirstName = firstName, LastName = lastName}; //if I use try-catch block, it says: The name 'firstName' doesn't exist in the current context


        Students.Add(newStudent);


        Console.WriteLine("The new student is added. \nEnter any key to return to main screen.");

        Console.ReadKey();

}



public static void SortStudents(IList<Student> students)

    {

        string temp;

        for (int i = 0; i < students.Count; i++)

        {

            for (int j = 0; j < students.Count; j++)

            {

                if (string.Compare(students[i].ToString(), students[j].ToString()) < 0)

                {

                    //swap

                    temp = students[i].ToString();

                    students[i] = students[j];

                    students[j] = temp; //error here

                }

            }

        }

        Console.WriteLine(students);

    }

学生班:


public class Student

{

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public int Age { get; set; }

    public string StudentNumber { get; set; }

    public string Gender { get; set; }

    public string FieldOfStudy { get; set; }

}

我正在尝试实现一种算法,该算法将按字母顺序对输入的名称进行排序,但它会在那里抛出错误。我该如何解决?我也在尝试使用 try-catch 块,但它抛出了我在代码中评论过的错误。提前致谢!


白衣非少年
浏览 173回答 2
2回答

忽然笑

正如一些评论员指出的那样,您可能需要访问Student和比较它们的属性,然后交换对象。所以像这样:public static void SortStudents(IList<Student> students)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //We change this to Type Student, not string.&nbsp; &nbsp; &nbsp; &nbsp; Student temp;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < students.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < students.Count; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //We look at the Properties of the object, not the Object.ToString()&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (string.Compare(students[i].FirstName, students[j].FirstName) < 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Here we are swapping the objects, because we have determined&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Their first names aren't in alphabetical order.&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = students[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students[i] = students[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; students[j] = temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //For loop, or Foreach loop here to iterate through your collection (ILIST)&nbsp; &nbsp; }

收到一只叮咚

是否有某种原因导致Student该类无法通过实现IComparable接口来实现这种“排序”逻辑?使用 aList<Student>来保存Student对象将使用此CompareTo方法对对象进行“排序”。这将允许类以任何你想要的方式“排序”。在这种情况下,它按姓氏然后按名字排序。你试过这样做吗?它可能看起来像下面...public class Student : IComparable {&nbsp; public string FirstName { get; set; }&nbsp; public string LastName { get; set; }&nbsp; public int Age { get; set; }&nbsp; public string StudentNumber { get; set; }&nbsp; public string Gender { get; set; }&nbsp; public string FieldOfStudy { get; set; }&nbsp; public int CompareTo(object obj) {&nbsp; &nbsp; Student that = (Student)obj;&nbsp; &nbsp; if (this.LastName.Equals(that.LastName))&nbsp; &nbsp; &nbsp; return this.FirstName.CompareTo(that.FirstName);&nbsp; &nbsp; return this.LastName.CompareTo(that.LastName);&nbsp; }}然后,“排序” aList<Student>将只是......Students.Sort();
打开App,查看更多内容
随时随地看视频慕课网APP