c#


Is there any technical reason to use or not to use var in C# when the type is known?


It seems that more and more C# code I read uses the var type identifier:
foreach (var itemChange in ItemChanges)
{
//...
}
instead of explicitly stating the type:
foreach (ItemChange itemChange in ItemChanges)
{
//...
}
even when the type is known.
I am still using the latter explicit version just because I think someone reading it later will more quickly understand which type a variable is than if you use var.
But is there any technical reason to use one or the other?
There is no technical reason. If the type cannot be inferred at compile time then the code will not compile.
You are right in stating there are instances where it may be better to use an explicit type for readabilty, e.g.
var obj = SomeMethod(); // what's the type? you'd have to inspect SomeMethod()
SomeClass obj = SomeMethod(); // the type is obvious
but other instances where using var makes perfect sense, e.g.
var obj = new SomeClass(); // the type is obvious
SomeClass obj = new SomeClass(); // the duplication of type is unnecessary
No, just readability.
In general no technical reason. Readability - in either direction - is the only real factor.
However, one small caveat is that var will infer the static type of the variable. If you want a sub or super class type you'll need to do the casting yourself. In the case of a foreach, as in your example, you can usually get downcasting performed for you "for free" just by declaring your loop variable with the subclass type.
The classic example is iterating over an XML NodeList that you know is a list of XmlElement, but Nodelist is typed as a collection of XmlNodes. Of course you can use a cast or an as to get back the type you want, but that would seem to defeat the purpose of using type inference :-)
Of course, the compiler will let you know about this as soon as you try to use a member of the node that is only available to XmlElement - so it's still not strictly a technical difference.
Another thing that is a little annoying is that if you use a tool like Resharper, it's very aggressive about suggesting you use var in every possible situation. It's particularly annoying when it recommends you change, for example, an int declaration into a var!
However, unless you turn that feature off, you'll get less "noise" from Resharper the more you use var.
The only technical reason I know of is that you can perform implicit casts without var, e.g.
int i = 5;
double a = i; // implicit cast with explicit types
but here I much prefer var as it makes the cast explicit; while I don't care so much about the types I do care when I'm performing a representation changing type conversion:
var a = (double)i; // explicit cast with implicit types
But really the general reason is readability, as you said. The question you need to ask yourself is why you think the exact concrete type is important for readability? Do you always write Linq queries calling out the concrete type, e.g.
from ItemChange itemChange in ItemChanges
// instead of
from itemChange in ItemChanges
Similarly, do you always call out the type arguments to generic methods instead of using type inference, e.g.
ItemChanges.Select<ItemChange, ItemChange>((ItemChange itemChange) => ...);
// instead of
ItemChanges.Select(itemChange => ...);
Or are you happy to let the compiler do some work for you and let it work out the types, at the 'expense' of not having the type information explicitly stated?
If you're happy with type inference in linq and generic methods, then you've already made the decision that you're OK with not having types explicitly spelled out everywhere, and you probably haven't found your code to be any less readable as a result (in fact, you've probably found quite the opposite). So using var is just another step down the same path that you're already on.
var is a C# 3.0 feature only mandatory in anonymous type
Because following code
var v = new { Amount = 108, Message = "Hello" };
dynamically create a new anonymous type, var usage is mandatory. For example, var is particularly useful in Linq where type are often created dynamically.
In any other situations, it's only a matter of taste for final application (it's resolved during compilation). But for code readers, I think 'var' is less informative that type itself.
No. Use var where it improves readability and vice versa.
var is a C# 3.x+ feature, isn't it? without using that, your code is more compatible with other versions too.
And there is lots of interesting questions in SO, when you search "var C#"
There is no technical reason to use var, other than anonymous types, in any given state of the program.
However, using var allows the program to change without you needing to edit it. Given
public int SomeMethod(){}
public List<T> SomeOtherMethod<T>(T parameter);
then
var x = SomeMethod();
var y = SomeOtherMethod(x);
Will work (and y will be List<int>). If you had used
int x = SomeMethod();
List<int> y = SomeOtherMethod(x);
then if SomeMethod() were changed to return long, then you'd have to change the definition of y.
I've seen this kind of thing propagate throughout a program, requiring hundreds of changes. The particular case was changing data access code to return ReadOnlyCollection<T> rather than List<T>. There were so many code changes required, that I changed all explicit mentions of List<T> to var, and the code will never need to change again.
Yes, they are different. var removes the original datatype and one datatype-check, so its more dangerous.
Type-checks
A normal variable definition includes a datatype-check on the initial assignment.
T a = init; // type-checked
a = b; // type-checked
a = c; // type-checked
This is missing when you use var
var a = init; // NOT type-checked
a = b; // type-checked
a = c; // type-checked
These compile-time tests are the glue which holds large programs together. Removing them is foolish.
Accidental changes
A normal variable definition has a fixed type - it cannot be changed accidentally.
TOrig a = init(e); // datatype fixed (as intended)
When you remove the initial datatype, you are allowing the variable's type to be changed accidentally. Any change to the initial expression can change the variable's datatype.
var a = init(e); // datatype changes with init and e
Over time, as the software is edited, the datatype could be accidentally changed. It could already have happened, and you cannot tell.
Example:
public double MaxValue = 1.2;
... meanwhile, in another source file ...
var x = MaxValue - 1;
var y = IsFull(x);
Both x and y's datatypes can be accidentally changing by editing MaxValue. Q. Has this already happened?
Summary
Using var turns the local variable's datatype into a unchecked moving part.
The more moving parts a machine has, the more likely it is to break.
Explicit datatypes prevent bugs.

Related Links

No endpoint listening wcf/rest
How to add NT AUTHORITY\IUSR to Administrators group
Combobox in Syncfusion XlsIO
Creating a Strong Name dynamically, without Sn.exe
Why is my locally declared variable not being recognized in the finally block?
Comparing two different types with Entity Framework
loginview shows “welcome system-name/administrator” and logout for the first time page load
How to abort another thread in .NET, when said thread is executing Console.ReadLine?
OrderBy / ThenBy looping - nested Lists in C#
How to login external web site from asp.net web page? [closed]
ListPicker for WPF?
How to notify user of new message?
WPF Treeview Bindings
How do you iterate through JSON object in an ASP.NET MVC controller?
Explicitly specifying the TaskScheduler for an implicitly scheduled method
DataGridView setting row height doesn't work

Categories

HOME
azure-documentdb
redux
botframework
weblogic
casting
sdk
pip
olap
vichuploaderbundle
xmlhttprequest
cakephp-2.0
pylint
project-reactor
beacon
printf
sonarlint
datasource
serenity-js
rndis
smali
pushbullet
ithit-webdav-server
ppp
mex
angular2-changedetection
boosting
launchd
typeorm
osgi-bundle
andengine
kubectl
oracle-nosql
webclient
openvms
matlab-compiler
flex3
android-databinding
anypoint-studio
const
motion
nsdateformatter
azure-web-roles
susy
aura-framework
taiga
risc
api-key
html-entities
spring-webflow-2
activeandroid
rivets.js
opos
remap
perldoc
critical-section
overhead
disassembly
lightspeed
therubyracer
fileopendialog
alarmmanager
git-push
airwatch
bytearray
jde
jmap
trendline
oryx
nslocalizedstring
nonblocking
jcomponent
tvp
textblob
callfire
photogrammetry
tidesdk
firebase-tools
jsonschema2pojo
api-eveonline
ios-enterprise
friendly-url
nx
moonscript
netzke
jquery-1.8
optimistic-concurrency
commonsware
cpack
html5-animation
cinder
fxcopcmd
subdirectories
manuals
opengl-to-opengles
content-delivery-network
stackless

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App