Is there a performance impact on the block level and the level at the .NET level.

I was looking at some C # code and came across some variables that were covered by a functional level that I would cover inside a code block (loop in this case) where they are used. It seems to me that it is close to being cleaner and easier to reason about, and it is reasonable enough to prefer a block-level region. But I was wondering if there is any significant impact on performance anyway?

+5
source share
1 answer

There is no difference in performance.

The scope of the variable is different from the lifetime of the variable. A variable is created on the stack stack for a function, regardless of whether it is declared in the function area or in a code block in the function. The variable exists during the execution of the entire function, only the compiler restricts access to the variable depending on its scope.

(Note that if different rules apply, if the variable is actually part of the closure instead of the usual local variable.)

+8
source

Source: https://habr.com/ru/post/1233973/


All Articles