Problem overview
A shopper searches for a product, and the page shows the top-rated brand that sells it. In this first version there is just one product and one brand, held in three variables: product (a string), brand (a string) and rating (an int). The program prints two lines. The first is the product name. The second is a ranked entry in the form 1. Brand (rating).
For a Laptop sold by TechPro with a rating of 4, the output is:
Laptop
1. TechPro (4)
Each line is first stored in its own variable, productLine and brandLine. The program then joins them with a newline and prints the result with a single Console.WriteLine call.
Approach: build each line as a string, then join
The task is all about strings. The first line is the product name unchanged. The second line is made of five pieces glued together: the rank "1. ", the brand, the text " (", the rating and the closing ")". Reading the output left to right gives you the order of the pieces.
The + operator joins strings. If one side is a string and the other is a number, C# converts the number to text and joins it, so rating can go straight into the expression with no conversion call. Spaces are part of the pieces: "1. " ends with a space, and " (" starts with one. That is where the spaces in 1. TechPro (4) come from.
Keeping each line in its own variable makes the last step trivial and keeps the building of the lines separate from the printing.
Step by step
- Create
productLineas astringand set it toproduct. The first output line is just the product name. - Create
brandLineas astring:"1. " + brand + " (" + rating + ")". Join the pieces with+in the order you read them. - Print both lines with one call. Join
productLine, the newline"\n"andbrandLine, and pass the result toConsole.WriteLine. The escape sequence\ninside a string is a single newline character: it makes the text continue on a new line where it sits.
The solution in C#
string product = "Laptop";
string brand = "TechPro";
int rating = 4;
string productLine = product;
string brandLine = "1. " + brand + " (" + rating + ")";
Console.WriteLine(productLine + "\n" + brandLine);
Details worth knowing:
- Strings are immutable.
+never changes an existing string. It creates a new one.productLine = productmakes both variables refer to the same text, and that is safe precisely because the text cannot be modified. - Left to right. The
+operator is evaluated from left to right.1 + 2 + "x"gives3x, because the two numbers are added first."x" + 1 + 2givesx12, because after the first join the left side is a string. In this lesson the expression starts with a string, so the rating is always joined as text. - Escape sequences.
"\n"is a newline. In a verbatim string, written with an@before the quote, backslashes are not special, so@"a\nb"prints a, a backslash, an n and b on one line. This lesson uses the normal kind. - Alternatives. C# also offers interpolated strings,
$"1. {brand} ({rating})", which build the same text. The lesson uses+to make each piece visible. Console.WriteLineends the line. The text passed to it is followed by one more newline, so the output ends cleanly after(4).
Edge cases and common mistakes
- Missing spaces. The most common error is writing
"1."without the trailing space, or"("without the space before it. The output must match exactly:1. TechPro (4). - Forgetting the newline. Joining the two lines without
"\n"printsLaptop1. TechPro (4)on a single line. - Quoting the variables.
"brand"in quotes is the word brand, not the variable's value. - Other data. The same code gives
Phoneand1. MegaCorp (5)when the variables hold "Phone", "MegaCorp" and 5.
Testing
- "Laptop", "TechPro", 4 give
Laptopand1. TechPro (4). - "Phone", "MegaCorp", 5 give
Phoneand1. MegaCorp (5).
