Declaring variables as late as possible and passing the returned method as a parameter

If I have a code like this:

string s = MyClass.GetString(); // Returns string containing "hello world";
ProcessString(s);

Is it slower than?

ProcessString(MyClass.GetString());

If so, why? In the second example, the compiler usually makes a variable from GetString (); method that returns a string?

Also, what's the point of declaring variables as late as possible? Does this get the advantage of GC? If so, how (I assume in terms of GC gens)?

thanks

+5
source share
1 answer

No, the compiler will produce an identical IL for both of these examples (not all examples like this, notice this example).

, # IL , , , CLR .

- . , , , , .

+10

All Articles