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# Querying With LINQ Now You're Querying Object Initialization

Refactor the LoadBirds method by using object initialization to add the pelican to the birds variable.

Keep getting "Did you forget the new keyword?". What am I doing wrong?

Bird.cs
using System.Collections.Generic;
using System.Linq;

namespace Treehouse.CodeChallenges
{
    public class Bird
    {
        public string Name { get; set; }
        public string Color { get; set; }
        public int Sightings { get; set; }
    }


}
BirdRepository.cs
using System.Collections.Generic;
using System.Linq;

namespace Treehouse.CodeChallenges
{
    public static class BirdRepository
    {
        public List<Bird> birds = new List<Bird>
        { 
            new birds { Name = "Pelican", Color = "White", Sightings = 3 }

        }
    }
}

3 Answers

Steven Parker
Steven Parker
208,481 Points

:point_right: It looks like the LoadBirds method is missing.

You still need the method, but you'll change how it works. Your object initialization itself is pretty close, but remember that the pelican is an object of type "Bird" (capital "B", no "s"). You'll also need to retain the method's return statement.

A refactored LoadBirds method might look like this:

        public static List<Bird> LoadBirds()
        {
            var birds = new List<Bird>
            {
                new Bird { Name = "Pelican", Color = "White", Sightings = 3 }
            };
            return birds;
        }

It works! Thank you very much for this explanation. I am having difficulties switching between the videos showing how it is done in the command line and typing out the challenges correctly when I get to them.

Jeff,

I totally agree with you the command line vs. code editor is super confusing I wish they would just use visual studio.

Jake Kennard
Jake Kennard
4,493 Points

I found this a really weird challenge for this point in the course... like wouldn't it be better to use constructors here instead? and what does it have to do with lists?