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) Console I/O Formatting Output

Sebastian Amhøj
Sebastian Amhøj
280 Points

Can't seem to figure out the last task of "Code Challenge: Formatting Output"

I've been trying to figure this one out for a bit, but can't seem to get it right. So i thought i would seek some help!

The Question is "Use the concatenation operator + to append “ rocks!” to the end of the contents of the firstName variable and print it to the screen using Console.WriteLine"

Please be nice on me :)

CodeChallenge.cs
string firstName = Console.ReadLine();
Console.WriteLine(firstName);

3 Answers

Steven Parker
Steven Parker
231,269 Points

So starting with the output line you have already:

Console.WriteLine(firstName);

So then if you "Use the concatenation operator + to append " rocks!" to the end...":

Console.WriteLine(firstName + " rocks!");
Stuart Wright
Stuart Wright
41,120 Points

I've added an extra line to your code which adds " rocks!" to the end of your variable.

string firstName = Console.ReadLine();
firstName = firstName + " rocks!";
Console.WriteLine(firstName);
Steven Parker
Steven Parker
231,269 Points

If you want to permanently modify firstName you could do it more concisely with a concatenation assignment operator:

firstName += " rocks!";
Sebastian Amhøj
Sebastian Amhøj
280 Points

Thanks a lot, because of an earlier on the same subject i got confused and used two "+" signs, one on each side :x

Stuart Wright
Stuart Wright
41,120 Points

Steven - I actually tried that first on the code challenge, thinking it would work, and it failed for me:

string firstName = Console.ReadLine();
firstName += " rocks!";
Console.WriteLine(firstName);
Steven Parker
Steven Parker
231,269 Points

For some reason, the challenge validator doesn't accept that, but it works fine in actual code! But I favor the solution that doesn't modify the variable, since the instructions are a bit ambiguous and the validator accepts it either way..