I have a C# 4.0 application and inside this application I have lots of unnecessary variables. Like _foo variable inside the below code:
public string Foo() {
var _foo = "foo bar";
return _foo;
}
As I mentioned, situations like this one occur inside lots of methods.
Would lots of unnecessary variables (like the ones I explained in this case) cause performance problem in C#?
Edit:
I am not asking any advice if I should remove them or not. My question is all about performance effect. In fact, the app is not written by me and I am new to this project. I just saw and wonder if it has any perf. effect besides the fact that it has a effect on the quality of the code.
No, they won’t.
The compiler is intelligent enough to remove all the unnecessary stuff when you compile in Release mode and optimize your code to:
or more precisely to:
which when compared to its Debug mode counterpart is quite different:
It’s definitely not something that you should worry about from a performance point of view. What you should worry about is readability of your code and this optimized code example seems far more readable than your version.
So you could trust the compiler which is constantly improving and is able to optimize such situations.