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
173,958 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,069 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
173,958 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,069 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
173,958 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..