Skip to content

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

@transbot

Description

@transbot

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions