C-SHARP - 1.5 Exercise: Display
After the theory of the previous video, time to practise. The exercise: print "Bonjour, il fait très beau aujourd'hui" on a single line, but using two different function calls — that is, "Bonjour, " and "il fait très beau aujourd'hui" each handled by its own call. Then below, on a new line, print "Oui effectivement, c'est un temps pour aller faire du sport". Pause the video, try it, then come back for the correction.
To get both halves on the same line, you need a function that does not add a newline at the end. Console.WriteLine adds one; Console.Write does not. So the first half is Console.Write("Bonjour, "); (mind the trailing space — the computer will not insert one for you), and the second half is Console.WriteLine("il fait très beau aujourd'hui");. WriteLine on the second call adds the newline that lets the next sentence appear underneath.
Key takeaways
- Spaces matter: omit the space after the comma and the two halves are glued together with no separator.
Console.WritevsConsole.WriteLine: the only difference is the trailing newline added byWriteLine.- Anticipate the next line: ending with
WriteLineon the first sentence saves you from having to print a manual"\n".
The final step prints the second sentence with Console.WriteLine("Oui effectivement, c'est un temps pour aller faire du sport");. Run the program and you get the first sentence on line 1 — assembled from two calls — and the second sentence on line 2. This drill makes the difference between Write and WriteLine concrete, and it forces you to think about whitespace because the compiler will never add it for you.