Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Collections Lists Sorting Lists

Sean Gibson
Sean Gibson
38,363 Points

Sorting Lists - will not run

I have typed the code in my workspace exactly as in the video, and it compiles fine, but when I run it nothing happens. Just a new line in the Console. What am I missing? I even tried adding Console.Read() and Console.ReadLine() at the end but that didn't work, either.

namespace Treehouse { class Student : System.IComparable<Student> { public string Name { get; set; } public int GradeLevel { get; set; }

public int CompareTo(Student that)
{
  int result = this.Name.CompareTo(that.Name);

  if(result == 0)
  {
    result = this.GradeLevel.CompareTo(that.GradeLevel);
  }
  return result;
}

} }

using System; using System.Collections.Generic;

namespace Treehouse { class Program { static void Main() { List<Student> students = new List<Student> { new Student() {Name = "Sally", GradeLevel = 3}, new Student() {Name = "Bob", GradeLevel = 3}, new Student() {Name = "Sally", GradeLevel=2} };

        students.Sort();

        foreach (Student student in students)
        {
            Console.WriteLine($"{student.Name} is in grade {student.GradeLevel}");
            Console.ReadLine();
        }
      }
}

}

6 Answers

Ben Reynolds
Ben Reynolds
35,170 Points

Any error messages or error highlights on any of the brackets?

Sean Gibson
Sean Gibson
38,363 Points

No, all seems fine. No compile errors, no run errors. Just moves to a new blank line in the console when I run it.

Sean Gibson
Sean Gibson
38,363 Points

I even copied and pasted the code from the Downloads section but still the same result.

Sean Gibson
Sean Gibson
38,363 Points

Seems to be a problem with the Workspace - I went into the code and changed it in ways that I knew would render it inoperable, but the console simply showed a new line like before.

Hello!

There's only one line of code showing because after each student you show you wait for input (Console.ReadLine()). You usually want to use ReadLine() when coding on your local computer and the program closes without letting you see the results.

You can either remove Console.ReadLine() or press enter after each student message is written to the console.

Hope that helped!

Sean Gibson
Sean Gibson
38,363 Points

Thanks but it wasn't showing anything. I tried the Console.ReadLine(), as I mentioned in my original post. And even if I entered no code, just gibberish, it showed no errors. So, I'm pretty sure it was an issue with the Workspace.