1

In my understanding C# started as a static language and with some enhancements in the .NET framework and started supporting the dynamic nature of the language.

I think the "var" keyword in c# is very powerful when it comes to loading the DLLs at the runtime and we do not know the types coming our way, it is very helpful.

But I think it brings the overhead of determining the type of the variable if the variable has been declared as a var at design-time. Now, re-sharper which is responsible for making our code much more nicer and cleaner by doing some nice suggestions has suggested me something like below:

My code looks like this:

StatusResult result = new StatusResult();

Resharper suggests that it has to be converted into

 var result = new StatusResult();

Why is that? why should I buy resharper's suggestion when I think that it is not a good idea? Or may be I am wrong?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Lost
  • 12,007
  • 32
  • 121
  • 193
  • You can change that behavior in the resharper settings if it does not fit your "convention". Neither is write or wrong, but you want to be consistent in your code base. Many people like `var` because it's usually obvious what the type is (or easy to find out using the tools in the IDE) – Caleb Mar 27 '14 at 23:57
  • 1
    Here is where the option is (resharper 8): Resharper -> Options -> Code Inspection -> Inspection Severity -> c# -> Language Usage Opportunities -> Use 'var' keyword ... – Caleb Mar 28 '14 at 00:00
  • 1
    Californicated, note that you may be misunderstanding what `var` means - "compiler please figure type for me, I'm too lazy to type it myself". I'm not exactly sure what your understanding of `var` is, but I suspect you think it is `dynamic` (which is totally different concept), see http://stackoverflow.com/questions/961581/whats-the-difference-between-dynamicc-4-and-var – Alexei Levenkov Mar 28 '14 at 00:02
  • Also: http://stackoverflow.com/questions/1873873/why-does-resharper-want-to-use-var-for-everything?rq=1 – Benesh Mar 28 '14 at 00:26
  • `var` is not dynamic typing. It's static typing, but the compiler determines the type at compile-time (not at runtime). So your entire question has no basis in reality. – John Saunders Mar 28 '14 at 00:28

2 Answers2

4

This is mostly an opinion-based question, but there's one issue worth addressing: it is very important to distinguish type inference from dynamic typing.

In dynamic i = 0; , i is declared as a dynamic variable, whose type is only resolved in run time. It may result in an overhead.

In var i = 0; i is declared as an int. The var keyword means only that the variable type will be inferred by the compiler at compile time. There's no "design-time" type inference overhead, since var i = 1; and int i = 1 compiles to the same set of instructions. It's got nothing to do with dynamic typing.

Benesh
  • 3,398
  • 1
  • 18
  • 38
3

As to your question,

Why is that? why should I buy resharper's suggestion when I think that it is not a good idea?

Here is my opinion from my experience. Some pro's of accepting this particular suggestion:

  • It aids in refactoring because StatusResult only occurs once
  • It shortens the line and removes a redundant declaration that is easily inferred by someone reading the code
  • It is less typing and the line is written faster when coding

Now, if the line of code was:

var result = GetStatusResult();  // A method call

I personally would not use var because a maintainer now needs to grok GetStatusResult() to see what var is.

At the end of the day it is a personal decision, unless you have to follow a coding standard, in which case you should lobby to change the standard or just go along with it.

As a side note, and as Benesh mentions below, var != dynamic. I think this is where var get's a bad rap. Plus the overuse in the example of when not to use it I provided above.

Eric Scherrer
  • 3,328
  • 1
  • 19
  • 34