8

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.

5
  • what is lots for you? 100 or 1.000.000 ? and if those are unnecessary what is preventing you to remove them? what kind of application is yours? Commented Jan 26, 2012 at 13:17
  • If you know they are unnecessary then remove them. Commented Jan 26, 2012 at 13:19
  • 6
    Those unused variables sure would waste brain cycles of the maintenance developer Commented Jan 26, 2012 at 13:19
  • @Ramhound you are missing the point of the question. See my update. Commented Jan 26, 2012 at 13:43
  • @DavidePiras about 500 times. See my updated question. This one is a class library which serves as a Data Access Layer. Commented Jan 26, 2012 at 13:45

3 Answers 3

26

Would lots of unnecessary variables (like the ones I explained in this case) cause performance problem in C#?

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:

public string Foo() { return "foo bar"; } 

or more precisely to:

.method public hidebysig instance string Foo() cil managed { .maxstack 1 .locals init ( [0] string str) L_0000: ldstr "foo bar" L_0005: stloc.0 L_0006: ldloc.0 L_0007: ret } 

which when compared to its Debug mode counterpart is quite different:

.method public hidebysig instance string Foo() cil managed { .maxstack 1 .locals init ( [0] string _foo, [1] string CS$1$0000) L_0000: nop L_0001: ldstr "foo bar" L_0006: stloc.0 L_0007: ldloc.0 L_0008: stloc.1 L_0009: br.s L_000b L_000b: ldloc.1 L_000c: ret } 

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It is worth to mention The C# compiler removes unused local variables when the optimize option is enabled. otherwise Unused local variables and unnecessary assignments increase the size of an assembly and decrease performance. learn.microsoft.com/en-us/visualstudio/code-quality/…
1

Unless you are having noticeable performance issues with the system, you should never code/modify for performance. As a software developer our first goal is to write clean, readable, elegant code. Like Darin said above compilers will do its job of optimizing for machine execution on our behalf.

Comments

1

If this is an unused variable, it might cause decrease performance and increase the size of the assembly since they will occupy memory spaces in run time.

For further reading and reference, you can check:

https://msdn.microsoft.com/en-us/library/ms182278(v=vs.140).aspx

1 Comment

It is worth to mention The C# compiler removes unused local variables when the optimize option is enabled. otherwise Unused local variables and unnecessary assignments increase the size of an assembly and decrease performance. learn.microsoft.com/en-us/visualstudio/code-quality/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.