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

Michael Thompson
Michael Thompson
7,427 Points

I have no idea what to do here

I'm lost completely

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);
        }
    }
}

1 Answer

Arturo Ortiz
Arturo Ortiz
1,502 Points

Hello Michael. In this exercise you have to solve 2 problem.

  1. The varaiable "input" is not being declare. Is already initialize but it wont compile if it doesnt have a type to declare it. So you must declare it of type "string" since you are getting input from the user. Answer: string input = Console.ReadLine();

  2. The variable "output" is being declared and initialized 2 times in different scopes. The problem with this is that the variable "output" will only be visible in the "if" and "else" scope and after that it will be destroyed and you would not be able to print the value of the "output" variable at the end of the exercise. In order to correct this you need to declare the variable in the "main" scope so it can be accessible to all the other scopes within "main" . Answer: string output;