Is using var actually slow? If so, why?

I learn C # and .NET, and I often use the keyword varin my code. I got an idea from Eric Lippert, and I like how this improves my code performance.

I’m curious, though ... a lot has been written on blogs about slow heap links, but I don’t observe this myself. Is it really slow? I mean slow compilation time due to type input.

+3
source share
5 answers

You declare:

I mean slow compilation time due to type 'inferencing'

. , ( ) . (, , ).

; , # ( ).

... .

+15

'var' # VARIANT, VB. var - , . . - , :

var x = new X();

,

X x = new X();

, . , "" (, ) .

+14

Var . dynamic?

+7

"" "", ( ) : (1) "" (2) "" .

, .

, "" . - , , , API. "" ( API ).

If you are talking about " var", then this is just a convenient way to tell you: "Compiler, enter the correct type here" because you do not want to do this work, and the compiler should be able to understand this. In this case, " var" does not constitute an "execution option", but rather the syntax of the source code specification.

+2
source

The compiler infers the type from the constructor.

var myString = "123"; no different from string myString = "123";

Also, generally speaking, reference types live in heap types and values ​​that are on the stack, regardless of whether they are declared using var.

+2
source

All Articles