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) Perform if / else

I don't understand what I am doing wrong. The error shows up lines that I don't even have

I added the else statement and then followed that with System.Console.WriteLine(language +\"is not C#\"); and it compiles errors for lines I don't even have.

CodeChallenge.cs
string language = Console.ReadLine();

if(language == "C#")
{
  System.Console.WriteLine("C# Rocks!");
}
else
{
  System.Console.WriteLine(language + \"is not C#\");
}

3 Answers

string language = Console.ReadLine();

if(language == "C#") { Console.WriteLine("C# Rocks!"); } else { Console.WriteLine(language + " is not C#"); }

Take out the backslashes and put a space between the first quotation and the word "is" in the else statement.

The backslashes are causing the compiler to see Console.WriteLine(language + is not C#); which doesn't make sense.

Also, the space is needed to pass the challenge.

Finally, the challenge could be written better. For consistency, either all of the Console.WriteLine/ReadLine statements should have System before it or none of them should have System before it.

So you have two mistakes in one place and that's the last thing you are trying to print out on the console

System.Console.WriteLine(language + \"is not C#\");

The first thing is the way you are adding whatever the string language has in the wrong way. The way you do it in C# is

System.Console.WriteLine("{0} is not C#", language);

The way you do it in C# is that you add {0} and then after the quotation mark of whatever you want to print you assign the number 0 and replace it with whatever variable you have. So let's say you have two variables and you want to print both of them the way you would do it is

string car = "BMW"
string model = "M5"
System.Console.WriteLine("The car is {0} and the model is {1}", car, model);

As you can see I have 0 and 1 that is because I have two variables, so if let's say you have 5 variables then you would have from 0 to 4, remember how programming languages count? They start from 0 and not 1.

Alan Mattanó
PLUS
Alan Mattanó
Courses Plus Student 12,188 Points

OK, text must be encapsulated between quotes.

"My text"

or

"is not C#"

You can concatenate more text by using +

"My text" + "is not C#"

language + "is not C#"

But if you need to print in console the quotes you can use **

" Press the letter \"Q\" to quit."

The result in console will be:

Press the letter "Q" to quit.