Writing better code
Now that you have learned the fundamentals of the C# language, let’s see some ways that you can write better code.
Treating warnings as errors
A simple yet effective way to write better code is to force yourself to fix compiler warnings. By default, warnings can be ignored. You can ask the compiler to prevent you from ignoring them.
Let’s review the default experience and then see how we can improve it:
- Use your preferred code editor to add a Console App/
console
project namedWarningsAsErrors
to theChapter06
solution/workspace. - In
Program.cs
, modify the existing statements to prompt the user to enter a name and then say hello to them, as shown highlighted in the following code:// See https://aka.ms/new-console-template for more information Console.Write("Enter a name: "); string name = Console.ReadLine(); Console.WriteLine($"Hello, {name} has {name.Length} characters!");
- Build...