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

Dominick Bartenope
Dominick Bartenope
2,092 Points

Not quite sure what I'm doing wrong

I declared input as a string variable and then declared the output variable in the main method but I don't understand why it is coming up as incorrect

Program.cs
using System;

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

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

            Console.WriteLine(output);
        }
    }
}

3 Answers

Steven Parker
Steven Parker
231,248 Points

A variable should only be declared once. It can be assigned many times, but in a plain assignment the name will not be preceded by the type.

Ben Reynolds
Ben Reynolds
35,170 Points

Since you've declared "output" in Main and made it a string, you don't need to use "string" before the variable name in the if and else blocks. It thinks you're trying to declare a new variable called output in each of those lines but it already exists.

Dominick Bartenope
Dominick Bartenope
2,092 Points

Thanks Steven, that helped me figure it out. Cheers!