IntelliTect / EssentialCSharp

This project contains the source code for the book Essential C# by Mark Michaelis (Addison-Wesley).
https://essentialcsharp.com/
MIT License
387 stars 159 forks source link

Listing 2.30: out variable still avaiable out of the if block #118

Closed transbot closed 3 years ago

transbot commented 4 years ago

P.75 "The result is that the number variable is available from both the true and false consequence of the if statement but not outside the if statement."

In fact, the variable is still in scope outside of the if block.

            // double number;
            string input;
            System.Console.Write("Enter a number: ");
            input = System.Console.ReadLine();
            if (double.TryParse(input, out double number))
            {
                System.Console.WriteLine($"input was parsed successfully to {number}.");
            }
            else
            {
                // Note: number scope is here too (although not assigned)
                System.Console.WriteLine("The text entered was not a valid number.");
            }

            System.Console.Write($"the value of number is {number}.");

temp

How do I modify the statement of the text?

MarkMichaelis commented 4 years ago

So sorry about the delay. I am just wrapping up C# 8.0. 😊 Please don't hesitate to provide feedback.

I have this change already in the next edition and, thanks to your email, I have updated the Erratahttps://github.com/IntelliTect/EssentialCSharp/blob/v7.0/Essential-C%23-v7.0-Errata.md as well.

Mark