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

I have been stuck on this for days and can't get to pass. Please no more hints. I need help.

What am i doing wrong?

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

1 Answer

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

Hi there! You're doing pretty well in my opinion, but let's take a look at the compiler error.

Program.cs(8,37): error CS0029: Cannot implicitly convert type void' tostring' Compilation failed: 1 error(s), 0 warnings

This is due to this line:

string output = Console.WriteLine();

The variable output is declared as being of type string, but Console.WriteLine(); returns a type of void. It returns nothing. Remember, WriteLine is simply for displaying something to the console.

Instead, the solution would be to initialize the string as empty and then change the values according to what the user input.

So, if I change that line to this, your code passes:

string output = "";

This declares a string named output and initializes it with an empty string. Later on, we'll give that output variable a different string value based on what we got in from the ReadLine() function.

Hope this helps! :sparkles:

Thank you so much.