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

Jim Conachan
Jim Conachan
2,514 Points

ignoring a C# key word in a string

I'm tasked with the following and obviously I need to ignore the fact that "is" and "not" are keywords. I thought I was supposed to use the slash to ignore something like this.

Here's the question: Print “C# Rocks!” if language equals “C#” otherwise print language + “ is not C#.” So if I entered "Cheese"then "Cheese is not C#" would be printed to the screen.

//Here's my code: string language = Console.ReadLine();

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

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

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

3 Answers

andren
andren
28,558 Points

I'm tasked with the following and obviously I need to ignore the fact that "is" and "not" are keywords.

Actually you don't need to ignore that, strings are treated as pure text. C# keywords are not given any special meaning inside a string and thus does not need to be escaped.

The issue with your code is that the quote marks are wrong. C# only accepts standard quote marks (") you use special stylized quote marks in your solution. I assume you copied the text from the challenge instructions since stylized quote marks are used in it.

If you replace them with regular quote marks like this:

string language = Console.ReadLine();

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

Then your code will work.

Also while it's not necessary to escape anything in this challenge it's worth mentioning that you are indeed correct in that \ is the character used to escape special symbols. But the only symbol that needs to be escaped in a regular string are quote marks meant to be used inside the string, escaping is not needed for any regular text.

Jim Conachan
Jim Conachan
2,514 Points

Andren,

Thank you so much. I copied the " is not C#." from the original question. I'm sure it'll work fine now. Thanks again!

Jim

Jose Lopez
Jose Lopez
4,615 Points

I see that you have an answer to your question but I just thought you might like to know that you could also write Console.WriteLine("{0} is not C#", language);

I feel that this approach makes it easier to read.

andren
andren
28,558 Points

As far as string interpolation goes I prefer the newer (C# 6 and above) syntax which is like this:

Console.WriteLine($"{language} is not C#");

Simply place a $ mark before the string and you can put the variable name directly into the brackets. That's easier to type and to me looks cleaner than the older syntax.

Jose Lopez
Jose Lopez
4,615 Points

Wow thanks i did not know you could do that

Jim Conachan
Jim Conachan
2,514 Points

Thanks Jose! That's great info. I'm at the beginning stages of learning C#, so not got there yet, but now I'm in the know!