Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 9
    I also use 'var' in foreach statements where I only care about enumerating a collection, not necessarily about the type of each item. Commented Feb 2, 2011 at 18:32
  • 1
    And as Jesse pointed out anonymous types ;) Commented Feb 2, 2011 at 18:43
  • 4
    @AnnaLear sometimes you have to care though. foreach (var row in datatable.Rows) in this case var is an object not a DataRow as one would expect. Commented Nov 9, 2011 at 18:47
  • @ThanosPapathanasiou Sure, yeah. Same thing goes for the Controls collection in Windows Forms and probably other situations, too. Commented Nov 9, 2011 at 18:51
  • I usually use var except when i want to be sure of the type of my variable (like when i want to be sure that the value is double for example) Commented Mar 7, 2013 at 16:49