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
201,355 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?