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# Querying With LINQ Now You're Querying Using Query Syntax

Emilio Meira
Emilio Meira
11,059 Points

Complaining about a public method creation...

Guys,

Sorry, it might be silly but I already checked a million times! I'm trying to create a public method inside the class but I'm getting an error like if the syntax is wrong, what can I be doing incorrectly?

Thanks a lot,

Emilio

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

namespace Treehouse.CodeChallenges
{
    public class NumberAnalysis
    {
        private List<int> _numbers;
        public NumberAnalysis()
        {
            _numbers = new List<int> { 2, 4, 6, 8, 10 };
            public IEnumerable<int> NumbersGreaterThanFive()
            {
                IEnumerable<int> results = from n in _numbers where n > 5 select n;
                return results;
            }
        }
    }
}

1 Answer

andren
andren
28,558 Points

The syntax error comes from the fact that you have placed the method not just inside the class, but inside the class' constructor. You can't place method declarations inside other methods/constructors (at least not using the normal method syntax).

If you move the method outside of the constructor like this:

using System.Collections.Generic;
using System.Linq;

namespace Treehouse.CodeChallenges
{ 
    public class NumberAnalysis
    { // The class begins here
        private List<int> _numbers;
        public NumberAnalysis()
        { // The class constructors begins here
            _numbers = new List<int> { 2, 4, 6, 8, 10 };

        } // The class constructors ends here

        public IEnumerable<int> NumbersGreaterThanFive()
        {
            IEnumerable<int> results = from n in _numbers where n > 5 select n;
            return results;
        }
    } // The class ends here
}

Then your code will work.

Emilio Meira
Emilio Meira
11,059 Points

That worked, thanks a lot! :)