var for locals

  • Thread starter Thread starter Peter Morris
  • Start date Start date
P

Peter Morris

var myObj = new MyClass();

What is the purpose of using "var" instead of "MyClass"?
 
Peter said:
var myObj = new MyClass();

What is the purpose of using "var" instead of "MyClass"?

In this case: none.

In general var means that the compile determines the type.

And it is most useful with the nameless types being
returned from LINQ.

Arne
 
var myObj = new MyClass();

What is the purpose of using "var" instead of "MyClass"?

Not much, really - it saves a bit of typing, more so in examples like

var wordsStartingWithSameLetter = new Dictionary<char, List<string>>();

var really shines in context with (and I guess was invented for) LINQ though -

e.g. (from the MSDN "anonymous types" example):

var productQuery =
from prod in products
select new { prod.Color, prod.Price };

foreach (var v in productQuery)
{
Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}


You have to use "var" here since the type "{ prod.Color, prod.Price }" is
created on the fly by the compiler - you do not know its name. This lets you
change your mind about what you want in the select clause without having to
update (or even create in the first place) a corresponding "data container"
class.

Regards,
Gilles.
 
This is what made me ask:

http://codebetter.com/blogs/jeffrey...controller-actions-should-be-unit-tested.aspx


In which he has code like

var repository = new FakeRepository(); var mockViewEngine = new
MockViewEngine(); var controller = new MainController(repository,
mockViewEngine);
Unlike using "var" in LINQ queries I see no benefit here. In fact I see it
resembling my old BASIC days back in the 1980's

var a = 1;

var b = "2";

var c = a + b;



The answer is "12", not 3 or 12. I'll stick with strongly typed variables
unless using LINQ I think :-)







Pete
 
This is what made me ask:

http://codebetter.com/blogs/jeffrey.palermo/archive/2008/03/09/
this-is-how-asp-net-mvc-controller-actions-should-be-unit-tested.aspx


In which he has code like

var repository = new FakeRepository();
var mockViewEngine = new MockViewEngine();
var controller = new MainController(repository, mockViewEngine);

All of those seem reasonable to me. It's immediately clear what the
type is, because it's on the right hand side - and because the variable
names are clear. (The variable names don't have to explicitly specify
the type, of course, so long as they're clear in intent.)
Unlike using "var" in LINQ queries I see no benefit here. In fact I see it
resembling my old BASIC days back in the 1980's

var a = 1;

var b = "2";

var c = a + b;

I might use var for b, but not a or c.

a) Integer literals get trickier, because the type changes depending on
the size of the number.

b) String literals are obviously strings. No ambiguity there. A better
variable name would probably be more help than an explicitly typed
variable here.

c) In this case it's not immediately obvious what the type *or the
behaviour of the expression* is. Again, I'd say this is more due to the
names of the variables than anything else.
The answer is "12", not 3 or 12. I'll stick with strongly typed variables
unless using LINQ I think :-)

You're still using strongly typed variables with var. You're just not
using *explicitly* typed variables. There's a massive difference. I
suspect you may know this, but others may not - I think it's important
to spread the word that "var != Variant" :)
 
You're still using strongly typed variables with var. You're just not
using *explicitly* typed variables. There's a massive difference. I
suspect you may know this, but others may not - I think it's important
to spread the word that "var != Variant" :)

I know, but I just don't see a *benefit* to using "var" here except that it
is quicker to type, and seeing as I haven't used less than two fingers to
type since I was about 15 it doesn't really benefit me. How I hated typing
lessons at the time!


Pete
 
I know, but I just don't see a *benefit* to using "var" here except that it
is quicker to type, and seeing as I haven't used less than two fingers to
type since I was about 15 it doesn't really benefit me. How I hated typing
lessons at the time!

Typing isn't the issue. Information redundancy is the issue.

Does having the type name twice actually make the code clearer? I
wouldn't say it does. The same information is available mere
centimetres away, so including it again is redundant. Where's the
benefit?

Redundancy is the enemy of information density (to steal Eric's
phrase). You want to get as much information across to the reader with
the least about of fluff - which is one argument in favour of
object/collection initializers too, on a somewhat separate note.

Getting the same amount of information just as clearly across without
repeating yourself is a good thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top