Checking the final code after analysis
Following the analysis of the two extensions, we have finally solved all the issues presented. We can check the final code, as follows:
using System;
try
{
int variable = 10;
if (variable == 10)
{
Console.WriteLine("variable equals 10");
}
else
{
switch (variable)
{
case 0:
Console.WriteLine("variable equals 0");
break;
default:
Console.WriteLine("Unknown behavior");
break;
}
}
}
catch (Exception err)
{
Console.WriteLine(err);
}
As you can see, the preceding code is not only easier to understand, but it is safer and is able to consider different paths of programming since the default for switch-case
was programmed. This pattern was discussed in Chapter 17, Best Practices in Coding C# 9, too, which concludes that best practices can be easily followed by using...