Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Collections Lists Sorting Lists

Sean Gibson
Sean Gibson
38,358 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,358 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,358 Points

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

Sean Gibson
Sean Gibson
38,358 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.

Calin Bogdan
Calin Bogdan
14,921 Points

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,358 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.