4

Just the reading the C# coding notes and saw this code fragment (http://msdn.microsoft.com/en-us/library/ff926074.aspx). They recommend using var instead of int in this loop.

var syllable = "ha";
var laugh = "";
for (var i = 0; i < 10; i++)
{
    laugh += syllable;
    Console.WriteLine(laugh);
}

This strikes me as utterly bizarre and I can't think why you would want to do this. In a foreach loop, fair enough, but when there's a simple and obvious type available?

James
  • 1,764
  • 5
  • 31
  • 49
  • The question is: why not? I usw var like 90% in my code. It is obvious which type is used here. – Christian Sauer Jul 30 '14 at 09:28
  • 2
    Absolutely right. Do not replace simple types with var. Especially int. It makes your code more like a puzzle. Do you it for more complex types though, Like types with generics. – Dennis_E Jul 30 '14 at 09:30
  • In a word, clarity. Why make other people assume things? – James Jul 30 '14 at 09:30
  • 1
    The question (as it stands) invites opinions (see the answers so far). You could ask: What are advantages and disadvantages? – usr Jul 30 '14 at 09:32
  • See the existing discussion: http://stackoverflow.com/questions/41479/use-of-var-keyword-in-c-sharp. I picked one of multiple. – usr Jul 30 '14 at 09:33

4 Answers4

3

Its depends as per requirement. The readability is important indeed, And var does not always improve the readability. Consider an example:

var totalSum = PriceCalculator.GetTotalSum(order); 

How can I know what the totalSum is. Is it a decimal? or is it a Money class? How does it improve the readability? And when I see code:

decimal totalSum = PriceCalculator.GetTotalSum(order); 

I know that totalSum is a simple value and I can work with in accordingly. I know what to expect from it. Of course, there are situations when using var can make the code better (for example, when the type of the variable is too long). But there are situations when it's better to know what you are dealing with. So it all depends on a concrete situation.

Trisped
  • 5,705
  • 2
  • 45
  • 58
Janty
  • 1,708
  • 2
  • 15
  • 29
2

The compiler replaces the var with the desired type anyway, so the IL-Code is not touched at all.

I suggest (and read this many more times), to use it, when the Classname gets big.

        Dictionary<SuperLongClassName, AnotherSuperLongClassName> dict = new Dictionary<SuperLongClassName, AnotherSuperLongClassName>();
        var dict2 = new Dictionary<SuperLongClassName, AnotherSuperLongClassName>();

Since it is a help of the compiler, there is no wrong or right, but I generally use it only when the object is directly created and the name gets to big.

Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
1

Usage of var makes more sense for readability. When looking at a type with a long name, that is were one really sees the usage such as

var myInstance = new DatabaseContextProcessingShipData();

versus

DatabaseContextProcessingShipData myInstance = new DatabaseContextProcessingShipData();

the redundancy is removed by using the var and it is easier to read.

usr
  • 168,620
  • 35
  • 240
  • 369
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • 2
    In this example. yes, use var. But not when you're initializing a simple int or string. Then var makes the code LESS readable. – Dennis_E Jul 30 '14 at 09:31
0

Examples are usually short and to-the-point, in which case it might not make much sense.

In "real" code it helps when your types have long names, to save on typing (and because it's usually clear which type you'll get when iterating).

Benjamin Diele
  • 1,177
  • 1
  • 10
  • 26