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 trialJoe Mackey
20,418 PointsIn Teacher.cs add the following to the Teacher class:
I am confused on how and where to create the constructor Are there any suggestions on a good book to help me understand how to do this correctly?
using System.Collections.Generic
namespace Treehouse.CodeChallenges
{
public class Course
{
public int Id { get; set; }
public string Title { get; set; }
public int TeacherId {get; set;}
public string Description { get; set; }
public int Length { get; set; }
public Teacher Teacher {get; set;}
public ICollection<Courses> Courses {get; set;}
// A default constructor that initializes the Courses property to an instance of List<Course>
//Looking for some help in explaining how to do this
}
}
namespace Treehouse.CodeChallenges
{
public class Teacher
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
3 Answers
Joe Mackey
20,418 Pointsusing System.Collections.Generic;
namespace Treehouse.CodeChallenges
{
public class Teacher
{
public Teachers()
{
Courses = new List<Course>();
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Course> Courses { get; set; }
}
}
Am I on the right track?
James Churchill
Treehouse TeacherJoe,
For more information on class constructors, check out this video in our C# Objects course:
https://teamtreehouse.com/library/c-objects/objectoriented-programming/object-initialization
If you haven't already done so, you might go ahead and start at the beginning of the C# Objects course and work your way through it. It has a lot of great information about how to use classes in C#.
Thanks ~James
Jacek Bialek
18,225 Pointspublic Teacher()! no public Teachers()
James Churchill
Treehouse TeacherJames Churchill
Treehouse TeacherYes, that looks correct to me.