-5

Possible Duplicate:
Use of var keyword in C#

After discussion with colleagues regarding the use of the 'var' keyword in C# 3 I wondered what people's opinions were on the appropriate uses of type inference via var?

For example I rather lazily used var in questionable circumstances, e.g.:-

foreach(var item in someList) { // ... } // Type of 'item' not clear.
var something = someObject.SomeProperty; // Type of 'something' not clear.
var something = someMethod(); // Type of 'something' not clear.

More legitimate uses of var are as follows:-

var l = new List<string>(); // Obvious what l will be.
var s = new SomeClass(); // Obvious what s will be.

Interestingly LINQ seems to be a bit of a grey area, e.g.:-

var results = from r in dataContext.SomeTable
               select r; // Not *entirely clear* what results will be here.

It's clear what results will be in that it will be a type which implements IEnumerable, however it isn't entirely obvious in the same way a var declaring a new object is.

It's even worse when it comes to LINQ to objects, e.g.:-

var results = from item in someList
              where item != 3
              select item;

This is no better than the equivilent foreach(var item in someList) { // ... } equivilent.

There is a real concern about type safety here - for example if we were to place the results of that query into an overloaded method that accepted IEnumerable<int> and IEnumerable<double> the caller might inadvertently pass in the wrong type.

Community
  • 1
  • 1
chetan
  • 51
  • 3
  • 8
  • Using var `you don't have to figure out the type` seems like a very bad reason to use it... you're supposed to know what the type is, var is just a shortcut to avoid typing it – Developer Mar 20 '12 at 10:08
  • 1
    Did you just copy the question from @Henrik's duplicate comment? – Bali C Mar 20 '12 at 10:08
  • 1
    @Bali C: Yes, and if you look at his previous questions, you'll notice a rather disturbing pattern... – BoltClock Mar 20 '12 at 10:10
  • @BoltClock Is there anyway of flagging a user rather than comment if they are doing weird stuff like copying other questions etc..? Just curious – Burkely91 Jun 14 '15 at 14:46
  • @Burkely91: Just flag any of their posts. – BoltClock Jun 14 '15 at 14:48

5 Answers5

2

Personally I find the circumstances you describe far from questionable, since there is no point in repeating yourself unless you specifically want the static type of a variable to be different than the static type of the expression used to initialize the variable. For example:

IEnumerable<int> foo = new List<int>(); // It's IEnumerable on purpose

Furthermore, there are absolutely no type safety concerns with var. The point is not that the variable can be of "any" type. It is of a very specific type, but you simply do not care to spell that type out.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

I'm only using it as a place holder until I'm sure which datatypes I'm using.

Sure this is a short answer but I think it's pretty close that when you should use the var keyword.

CodingBarfield
  • 3,392
  • 2
  • 27
  • 54
0

the var keyword is used as shorthand in the language, but isn't a .NET type. The compiler must know the type of the variable to use the var keyword - so it is type-safe.

I personally only use it if the type name is also used in the assignment and the name is possible too long to duplicate in the code.

var dictionary = new Dictionary<string, string>();

It is also used for anonymous types (but still, the compiler must know the signature of the anonymous type).

var fred = new { Age = 23, Name = "Fred" };

This method is used commonly in the select clause of LINQ queries.

Connell
  • 13,925
  • 11
  • 59
  • 92
0

Just an "abstraction" or "syntax sugar" to be able to write a code without specifying first the type (this is no your first cases)

In second case: LINQ queries, instead, to rapresent some unknown, dynamic, not concrete, if you wish, type.

could be:

var results = from item in someList
              where item != 3
              select item; //item a class instance

could be

 var results = from item in someList
              where item != 3
              select item.ItemName; //string property of that class

could be

 var results = from item in someList
          where item != 3
          select new {item.ItemName, item.ID}; //unknown type dynamically generated, that conains the string and integer, like result
Tigran
  • 61,654
  • 8
  • 86
  • 123
0

As far as I know var remains strong typed. The compiler calculated what the proper type should be. In fact it has no real meaning.

It is only a trick to reduce the number of manipulations. For instance when you change a type in one class, this can result in a cascade of modification. But its only a way to migrate work from the programmer to the compiler.

For instance your Linq query will result in a type IEnumerable<TA>. When you change some class so the result will be IEnumerable<TB> there is no need to change this part of the code.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555