V
V
vovkaooo12021-08-02 13:30:09
C++ / C#
vovkaooo1, 2021-08-02 13:30:09

How does the declaration scope rule work in c#?

There is a misunderstanding of how the compiler determines whether a variable can be declared in a given scope or not.
First listing

static void Main(string[] args)
        {
            int some = 400;
            if (some > 100) 
            {
                int ansome = some - 100;
                Console.WriteLine(ansome);
            }
                int ansome = 10000;
                Console.WriteLine(ansome);
        }

In this case, the compiler will generate an error stating that you cannot declare a variable in the given scope because that name is used in the enclosing local scope to define a local variable or parameter.

Second listing
static void Main(string[] args)
        { 
            int some = 400;
            if (some > 100) 
            {
                int ansome = some - 100;
                Console.WriteLine(ansome);
            }
            {
                int ansome = 10000;
                Console.WriteLine(ansome);
            }
        }

In this case, the compiler will not generate an error. And everything seems to be logical, but I do not understand what the meaning is. The book that gives this example says that this rule was introduced to prevent programmers from making mistakes who work in conditions where variables are required to be declared at the beginning of the code. But if I move the variable to the beginning of the code, then all other initializations will immediately become erroneous. And here the question arises, what do I not understand? And what nuances are still hidden and where you can read about them.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya, 2021-08-02
@sarapinit

https://docs.microsoft.com/en-us/dotnet/csharp/lan...

The lifetime of a local variable is the portion of program execution during which storage is guaranteed to be reserved for it. This lifetime extends at least from entry into the block, for_statement, switch_statement, using_statement, foreach_statement, or specific_catch_clause with which it is associated, until execution of that block, for_statement, switch_statement, using_statement, foreach_statement, or specific_catch_clause ends in any way. (Entering an enclosed block or calling a method suspends, but does not end, execution of the current block, for_statement, switch_statement, using_statement, foreach_statement, or specific_catch_clause.) If the local variable is captured by an anonymous function (Captured outer variables), its lifetime extends at least until the delegate or expression tree created from the anonymous function,

Apparently, the compiler considers just scopes, regardless of the position of the variable in the block. It seems that there is no simple answer here and you have to get into the compiler code, or just put up with the specification

K
krka92, 2021-08-02
@krka92

The point is that we declare (and sometimes immediately initialize) a variable once and use it in the current and nested blocks.

static void Main(string[] args)
        {
            int some = 400;
            int ansome;
            if (some > 100) 
            {
                ansome = some - 100;
                Console.WriteLine(ansome);
            }
                ansome = 10000;
                Console.WriteLine(ansome);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question