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# C# Basics (Retired) Perfect Variable Scope

Aaron Lawrence
Aaron Lawrence
2,954 Points

Where is the Scope.

I think I know what is wrong. This might have something to do with the scope but I'm not entirely sure.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            input = Console.ReadLine();

            if (input == "quit")
            {
                Console.WriteLine("goodbye.");

            }
            else
            {
                string output = "You entered " + input + ".";
            }

            Console.WriteLine(output);
        }
    }
}
Mark Samonte
Mark Samonte
Courses Plus Student 5,845 Points

Hey Aaron,

You are right when you say that the code is not compiling due to scope. Try and declare the variables outside of the If statement. Also remember to declare the type of the variable. Hope that helps!

-MS

1 Answer

Dov Brodkin
Dov Brodkin
2,695 Points

Hi I think you forgot to declare your input variable, to do so either put var or string in front of the variable statement.