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

Variable Scope

Hey guys and girls, I am really stuck on this variable scope challenge cause I do not know what to do. Can someone help me?

Thanks

Program.cs
using System;

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

            if (input == "quit")
            {
                string output = "Goodbye.";
            }
            else
            {
                string output = "You entered " + input + ".";
            }

            Console.WriteLine(output);
        }
    }
}
Jon Wood
Jon Wood
9,884 Points

Antonio De Rose that's a great explanation! I like the idea of explaining with comments in the code.

2 Answers

Antonio De Rose
Antonio De Rose
20,884 Points
using System;

namespace Treehouse.CodeChallenges 
{ 
    class Program 
    { 
        static void Main() 
        {
            string input = Console.ReadLine();//this is correct, good work
            string output;//when you declare the variable here, accessibility of this variable is high, from here
            if (input == "quit")
            {
                output = "Goodbye.";//Now that the output is declared, you cant declare here again, so I take off the string part
            }
            else
            {
                output = "You entered " + input + ".";//here too, I take the string part, otherwise, am double doing
            }

            Console.WriteLine(output);
            //till here, the output variable could be accessed
        }
    }
}
Antonio De Rose
Antonio De Rose
20,884 Points
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {            
            input = Console.ReadLine(); //variable being used without declaring, so you will need the variable type in front

            if (input == "quit")
            {
                string output = "Goodbye.";//this is about variable scoping, string is only within this blocks
            }
            else
            {
                string output = "You entered " + input + ".";//again variable scoping, string is only available within the blocks
            }

            Console.WriteLine(output);// given, it was declared, in those blocks, it cannot be used here, the output
//better idea would be declare the output variable just below, where input is to be declared
//then you need not have declare the variable output, at, if and else
        }
    }
}

Moderator Edit: Moved response from Comments section to Answers.

Thanks so much Antonio!

Hey Antonio, Thanks for the answer! However, I am still rn having trouble with the code. Here is what i have currently:

using System;

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

        if (input == "quit")
        {
            string output = "Goodbye.";
        }
        else
        {
            string output = "You entered " + input + ".";
        }

        Console.WriteLine(output);
    }
}

}