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 and Using a One-to-Many Relationship

A default constructor that initializes the Courses property to an instance of List<Course>

I do not understand how to write a default constructor. Will someone offer a book or tut to help me understand how to do this?

Course.cs
namespace Treehouse.CodeChallenges
{
    public class Course
    {
        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; }
    }
}
Teacher.cs
// A using directive for the System.Collections.Generic namespace
using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
    public class Teacher
        //  A default constructor that initializes the Courses property to an instance of List<Course> 
        // It is plain I do not understand how to write a default constructor that  initializes the Courses property to an instance of List<Course> 
        // I am not understanding ther video after watching often. Will some kind sole off a reading lesson that will help me to understand this?
        Courses = new List<Course>();
    {
        public Courses()

    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        // A navigation collection property named Courses of type ICollection<Course> 
        public ICollection<Courses> Courses { get; set; }

    }
}

3 Answers

Mr Clark, Thanks you for being civil and helpful. Not that common here. You posted answer works and I will spend some time figuring out why. I is appreciated more than you can imagine. Thank You Joe

Allan Clark
Allan Clark
10,810 Points

A constructor is a method on a class that shares the same name as the class and has no return type. 'Default' just means it takes no parameters. This is the method called when a new instance of the class is created. You seem to have the right idea just confused on the syntax. Try this:

public class Teacher { public Teacher() { Courses = new List<Courses>(); }

       public int Id { get; set; }
        // other properties 

}

I apologize I can't get the code format straight as I'm on mobile. Will edit later. Don't have any resources off the top of my head but if you need more I can see what I can dig up.

I have developed a mental Block for this question. Added you suggestion and I still cant get it to run. But your explanation helped a lot. Thanks

Allan Clark
Allan Clark
10,810 Points

may be running into some more syntax issues with the navigation property. The finished class should look like this. If you are still having trouble post the code and error so we can get a better idea of whats going on. (also make sure you are in the teacher class....made that mistake while checking myself haha)

using System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
    public class Teacher
    {

        public Teacher() { 
            Courses = new List<Course>();
        }

        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual ICollection<Course> Courses {get;set;}


    }
}