Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perform if

Balraj Duhre
PLUS
Balraj Duhre
Courses Plus Student 1,987 Points

Write code to print "C# Rocks!". string language = Console.ReadLine(); Why do I keep getting this wrong?

I've tried everything and I keep getting it wrong. I don't know if I misunderstand on what I'm supposed to do.

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

2 Answers

Calin Bogdan
Calin Bogdan
14,920 Points

Hello!

You have to use the if statement to check whether the input stored into the language variable equals "C#" and if it does you have to print the message.

Let's take it step by step:

// The first thing you write to the console will be stored here:
string language = Console.ReadLine();

// For example, if you type "Jamie", the value stored in the language variable will be Jamie.

// You have to check if the input (language variable) is equal to "C#"
// We can use the if statement to check this:
if (language.Equals("C#")) 
{
    // print "C# Rocks!"
}

// Well, even if the input equals "C#", it does nothing yet. Let's print "C# Rocks" if the input equals "C#"
// Finally, we use the Console.WriteLine() method to print the message

if (language.Equals("C#")) 
{
    Console.WriteLine("C# Rocks!");
}

Hope it helped!

Balraj Duhre
Balraj Duhre
Courses Plus Student 1,987 Points

Thank you. It worked. I didn't understand the question I guess. I never thought about using an if statement.

I dont understand why it doesnt work. Could you please explain again?

string language = Console.ReadLine(); if (language == "C#") { Console.WriteLine = ("C# Rocks!"); }

It gives me an error: <<StudentsCode.cs(10,12): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer. Compilation failed: 1 error(s), 0 warnings>>

Thank you