Are the following equivalent?
Is it important to use var type in certain circumstances?
Currently using this:
static int ReturnIDfromDb(string sqlString, string outputParam) {
var connectionString = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString;
using(var conn = new SqlConnection(connectionString))
using(var comm = new SqlCommand(sqlString, conn)) {
...
}
}
Is this not more specific?
static int ReturnIDfromDb(string sqlString, string outputParam) {
var connectionString = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString;
using(SqlConnection conn = new SqlConnection(connectionString))
using(SqlCommand comm = new SqlCommand(sqlString, conn)) {
...
}
}
For the compiler it is the same. Seriously.
varturns into hardcoded.But still, there are corcumstances where you HAVE to use var, because you DO NOT KNOW THE TYPE.
IQueryable(LINQ) allows you to project:…..
In this case you get an array of – anonymous types, so…
it has to be
var resultarray
because you CAN NOT name the type at this point.
But
varis a COMPILER ONLY thing. Code convention of Resharper says: use var, not type. I tend to agree these days.After the compiler it is the same.