-3

I'm new to c# and I'm totally against var.

Every time when I see var in code example, it makes me crazy, "what type is it?" even the semantic is obvious, I'd like to see the full type name. The presence of full type name makes me feel that the code is elegant and professional.

I know var can be necessary if using an anonymous type, but I'm against anonymous type too. If you want to use a type, why not give it a name? The following is some example of using var:

var results = context.People.Select(p => new {p.PersonID, p.Name});

But I think it should be written like this: (Don't argue about the definition or syntax, I'm not sure whether it's correct because I'm not familiar with linq, I just want to say "why not give it a name?")

class Result
{
    public int PersonID;
    public string Name;
    public Result(int id, string name)
    {
        PersonID = id;
        Name = name;
    }
}
Result results = context.People.Select(p => new Result(p.PersonID, p.Name));
Adel Khayata
  • 2,717
  • 10
  • 28
  • 46
Cherry
  • 153
  • 11

4 Answers4

2

First, let's fix your code:

class Result
{
    public int PersonID;
    public string Name;
    public Result(int id, string name)
    {
        PersonID = id;
        Name = name;
    }
}
IEnumerable<Result> results = context.People.Select(p => new Result(p.PersonID, p.Name));

And to be honest, it should be pretty apparent by now why many like 'var' and anonymous types - your version is significantly more verbose.

In particular in LINQ queries you often want a data type for a very limited scope - so the cost of defining a new data type is excessive and provides little value. Another situation I see a lot is where the anonymous type is used to be serialised (e.g. to json) - where having the data and field names written together makes for nice simple code.

new {Name = "Mike", Version = 1, Date = DateTime.Now}.ToJson();
NPSF3000
  • 2,421
  • 15
  • 20
  • Thanks for the fix. I think verbose is not bad. The verbose make code clearer to me - clearly that the result is another type. Maybe because my work seldom use query? – Cherry Jul 26 '13 at 04:07
  • Not being familiar with the syntax will hinder comprehension. Still 1 line vs 11 isn't a bad saving, especially if you use multiple anonymous types in a single method or query! – NPSF3000 Jul 26 '13 at 04:40
1

Anonymous types (and Tuples for that matter) make it easier to maintain your code without a hundred tiny classes created for the sole purpose of passing a couple values around between methods or even within a single method.

You can read some arguments for the use of both in an article by James Hare called "The Joy of Tuples and Anonymous Types".

As for var, I suppose that in most cases it's a matter of readability. In certain cases, it's easy to see which type var will be declared as.

SomeClassThatINeedToUse myInstance = new SomeClassThatINeedToUse();

var myInstance = new SomeClassThatINeedToUse();
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

When you work on a big application, you come across too many scenarios (writing LINQ queries) where you don't want to define a class for every small structure that you are about to arrive at.

Keep things simple!!

IMO, usage of keyword var is just a personal choice; but anonymous types - just too good to resist using.

Prash
  • 1,122
  • 1
  • 8
  • 10
0

Here's an old thread with much more discussion than you're likely to get, here: Use of var keyword in C#

Community
  • 1
  • 1
rutter
  • 11,242
  • 1
  • 30
  • 46