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

Bummer! i have an error

I do not understand this! can please someone give me the answer because i have already asked this question 2 times..

Program.cs
using System;

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

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

            Console.WriteLine(output);
        }
    }
}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The problem lies in your declaration of the output variable. The Console.ReadLine() returns a string from what the user inputs. But the Console.WriteLine() returns nothing. Remember, it's simply for printing something back to the user. The compiler can't handle this because it's returning type void back and trying to assign that to a variable that's supposed to hold a string.

You have this:

 string output = Console.WriteLine();

But that should be:

 string output;

This line declares a string named output. Later on, we'll change the value of that variable based on what the input value is, and that part is all correct.

Hope this helps! :sparkles:

[MOD: added c# formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Though the challenge instructions say Declare the output variable just before the if statement and assign it to an empty string (i.e. ""), both the following are acceptable answers:

string output;
// or 
string output = "";

Thanks guys!