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# Entity Framework Basics Extending Our Entity Data Model Defining a Many-to-Many Relationship

I need help with understanding how to initialize a list with a constructor. I have researched for hours on the Internet

In the Entity Framework Basics course, I am having trouble with understanding how to initialize a list with a constructor. I have spent a great deal of time researching how to do this on Stack Overflow and other sites, but have not been able to accomplish successfully passing the code challenges on Treehouse. Can you please help?

using System.Collections.Generic;

namespace Treehouse.CodeChallenges
    public class Course

        public class Students()
            List<Students>Students = new List<Students>();

        public int Id { get; set; }
        public int TeacherId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int Length { get; set; }

        public Teacher Teacher { get; set; }
        public ICollection<Students> Students {get; set;}

//            public Human(int id, string address, string name, params ContactNumber[] contactNumbers) : this(id, address, name)
//    {
//        ContactNumbers = new List<ContactNumber>(contactNumbers);
//    }
        // Using the first constructor:
//         List<ContactNumber> numbers = List<ContactNumber>() {
//         new ContactNumber(1),
//         new ContactNumber(2),
//         new ContactNumber(3)
//         };

//        A default constructor that initializes the Students property to an instance of List<Student>

namespace Treehouse.CodeChallenges
    public class Student
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

1 Answer

Steven Parker
Steven Parker
230,230 Points

A constructor always has the same name as the class and no type.

And remember that a default constructor takes no arguments. Then, initializing a field is just assigning it to a new instance of the type. So it might look like this:

        public Course() {
            Students = new List<Student>();

You also have an error in your declaration of the field where you wrote "ICollection<Students>" (with an extra "s") instead of "ICollection<Student>".