question on anonymous type

  • Thread starter Thread starter timor.super
  • Start date Start date
T

timor.super

Hi all,


I have a question on anonymous type

I can write :
using (StreamWriter writer = new StreamWriter(...))

and I can write this too :
using (var writer = new StreamWriter(...))

What is the best ? Which option should I choose ?
The first seems to be more readable ...

Thanks in advance for your answer
 
Hi all,

I have a question on anonymous type

I can write :
using (StreamWriter writer = new StreamWriter(...))

and I can write this too :
using (var writer = new StreamWriter(...))

What is the best?
Which option should I choose ?
It's a matter of taste usually.
The first seems to be more readable ...
Then use that. Personally, I don't like the duplication involved in
writing "StreamWriter" twice (and it gets worse when I want to say
Dictionary<string,List<Entry>> or some other equally complicated
generic) ... but it's up to you.

Note that var becomes more important when you start using LINQ
expressions (where the actual types can be rather difficult to write
down).
 
I can write :
using (StreamWriter writer = new StreamWriter(...))

and I can write this too :
using (var writer = new StreamWriter(...))

What is the best ? Which option should I choose ?

In this case I see no benefits in either, except "var" is quicker to type.
 
I have a question on anonymous type

Actually, you have a question on implicitly typed local variables.
Anonymous types are the types created when you write code like this:

new { Name="Jon" }

The two features are often used together, but don't have to be.
I can write :
using (StreamWriter writer = new StreamWriter(...))

and I can write this too :
using (var writer = new StreamWriter(...))

What is the best ? Which option should I choose ?
The first seems to be more readable ...

Well, the first contains redundant information - but it's more
expicit. It's largely a matter of taste though. I've found myself
using implicitly typed local variables quite a bit with no loss of
readability.

Jon
 
Thanks all for your answer.

That's mean that in IL language, both instructions are equivalent ?
 
Thanks all for your answer.

That's mean that in IL language, both instructions are equivalent ?

Yes. Don't forget that C# 3 still compiles to IL run by the 2.0 (or
2.0SP1) CLR. The new features of C# 3 don't require runtime support.
Anonymous types are just compiled into normal types with names which
are valid in IL but invalid in C#. Implicitly typed local variables
just have their types inferred by the compiler and used as normal.

Jon
 
Ok, thanks for your answer

Best regards

Yes. Don't forget that C# 3 still compiles to IL run by the 2.0 (or
2.0SP1) CLR. The new features of C# 3 don't require runtime support.
Anonymous types are just compiled into normal types with names which
are valid in IL but invalid in C#. Implicitly typed local variables
just have their types inferred by the compiler and used as normal.

Jon
 
Hi all,

I have a question on anonymous type

I can write :
    using (StreamWriter writer = new StreamWriter(...))

and I can write this too :
    using (var writer = new StreamWriter(...))

What is the best ? Which option should I choose ?
The first seems to be more readable ...

Thanks in advance for your answer

Hi,

I would go for the first one, in this case using an anonymous type is
unnecesary, somewhere I read a post that lamented that MS allowed such
a construction.
BTW, the above is not an anonymous type expression
 
 I would go for the first one, in this case using an anonymous type is
unnecesary, somewhere I read a post that lamented that MS allowed such
a construction.

Somewhere there is a line, and I don't claim to know where /exactly/
it is - but sometimes even in this scenario, "var" genuinely improves
the code. For example, what if it was a
KeyContainerPermissionAccessEntryCollection*, or maybe a
Dictionary<SomeLudicrouslyLongTypeName, AnotherLongTypeName>...
repeating all that lot in the same expression (which is a pre-
requisite for "var) is just fluff.

*=yes, this isn't IDisposable, but that isn't the point I'm trying to
make...

Marc
 
I have a question on anonymous type

I can write :
using (StreamWriter writer = new StreamWriter(...))

and I can write this too :
using (var writer = new StreamWriter(...))

What is the best ? Which option should I choose ?
The first seems to be more readable ...

I would go for the first as being more readable. Most
C# programmer will be used to reading the type first.

And you will not be typing the class name twice. The
IDE should propose it the second time.

Arne
 
I would go for the first as being more readable. Most
C# programmer will be used to reading the type first.

At the moment, yes. I think things will change as C# 3 becomes more
widely known.
And you will not be typing the class name twice. The
IDE should propose it the second time.

True, but it's not really about typing. It's about information
redundancy.

Ironically, the thing which the OP *didn't* do which I often would is
use a base type or interface:

using (TextWriter writer = new StreamWriter(...))

that adds information - it tells the reader that actually, any
TextWriter will do for what we need to call - it just so happens that
it uses StreamWriter at the moment. At the point you're adding this
extra information, there is no longer redundancy for implicit typing
to remove.

Jon
 
Jon said:
At the moment, yes. I think things will change as C# 3 becomes more
widely known.

I doubt it.

C# 3 will become widely known.

But most C# developers will have learned C# in earlier versions
and many of them will also use other languages from let us call
it "the C syntax family".
True, but it's not really about typing. It's about information
redundancy.

I can not see that redundancy as a big problem. The compiler
will catch it if inconsistent changes are made.

Arne
 
Arne Vajhøj said:
I doubt it.

C# 3 will become widely known.

But most C# developers will have learned C# in earlier versions
and many of them will also use other languages from let us call
it "the C syntax family".

Yes, I use Java day to day professionally now - and regularly miss
"var".

If people ignore the C#-specific features of C# 3 just because they're
not used to them, they'll be really missing out.
I can not see that redundancy as a big problem. The compiler
will catch it if inconsistent changes are made.

The problem with redundancy isn't the possibility for inconsistency -
it's the lack of information density. It takes more space redundantly
specifying information, so there's more to wade through when reading
the code.
 
Jon said:
Yes, I use Java day to day professionally now - and regularly miss
"var".

If people ignore the C#-specific features of C# 3 just because they're
not used to them, they'll be really missing out.

If the only think they miss out is stuff like this that has no
functional impact, then they will survive.
The problem with redundancy isn't the possibility for inconsistency -
it's the lack of information density. It takes more space redundantly
specifying information, so there's more to wade through when reading
the code.

The possibility of inconsistency is the classic reason to avoid
redundancy.

Disk space is cheap.

And I find it hard to believe that the usage of var instead of
explicit classname should take longer time to read.

Arne
 
Arne said:
I doubt it.

C# 3 will become widely known.

But most C# developers will have learned C# in earlier versions
and many of them will also use other languages from let us call
it "the C syntax family".

I think though that var is a special case because in some circumstances
its the only effective way to reference a type, anonymous types gained
from projection out of a linq query for example.


var report = from line in SomeEnumeration
select new
{
Value = line.SomeValue,
AnotherValue = line.AnotherValue
}

foreach (var reportline in report)
{
//...
}


I this will become common enough for var to be considered "normal" C#
syntax.

Cheers Tim.



--
 
Arne Vajhøj said:
If the only think they miss out is stuff like this that has no
functional impact, then they will survive.

I'm sure they'll survive - they just may not be as productive.
The possibility of inconsistency is the classic reason to avoid
redundancy.

In many other scenarios, yes. Not here.
Disk space is cheap.

Who was arguing that disk space was relevant?
And I find it hard to believe that the usage of var instead of
explicit classname should take longer time to read.

It changes the emphasis of the code. Eric Lippert puts it well:
http://csharpindepth.com/ViewNote.aspx?NoteID=61
 
Tim said:
I think though that var is a special case because in some circumstances
its the only effective way to reference a type, anonymous types gained
from projection out of a linq query for example.

var report = from line in SomeEnumeration
select new
{
Value = line.SomeValue,
AnotherValue = line.AnotherValue
}

foreach (var reportline in report)
{
//...
}

Sure - there are cases where var is necessary. I do not disagree
with that.

The question discussed is whether it should replace declarations
of well known types.

Arne
 
Jon said:
I'm sure they'll survive - they just may not be as productive.

I doubt it.

It has been discussed as a new feature for Java and it is nowhere
near the top of the popularity list.
In many other scenarios, yes. Not here.


Who was arguing that disk space was relevant?

You said that it takes more space.
It changes the emphasis of the code. Eric Lippert puts it well:
http://csharpindepth.com/ViewNote.aspx?NoteID=61

I think it is the other way around.

X o = new X();

emphasizes the what and not the how.

First you read that you have an o of type X.

Then you may or may not read where you got it from.

var o = new X();

First you read that you have an o without more info and
then you has to read where it came from to figure out what
it is.

Arne
 
Arne Vajhøj said:
I doubt it.

It has been discussed as a new feature for Java and it is nowhere
near the top of the popularity list.

That doesn't mean it won't prove to be useful in C#. People often
underestimate how useful a feature will be when they haven't had it.
Look up Blub's Paradox.
You said that it takes more space.

I was talking about *visual* space. *That* is important - disk space
(for source code) generally isn't.
I think it is the other way around.

X o = new X();

emphasizes the what and not the how.

First you read that you have an o of type X.

Then you may or may not read where you got it from.

var o = new X();

First you read that you have an o without more info and
then you has to read where it came from to figure out what
it is.

No, you're missing the point - with var, you tend to concentrate on
what you *do* with o rather than the exact type of o (which is part of
the "how").
 
Jon said:
That doesn't mean it won't prove to be useful in C#. People often
underestimate how useful a feature will be when they haven't had it.
Look up Blub's Paradox.

I don't think that apply very well. A rather big percentage of Java
programmers also program in C#.
No, you're missing the point - with var, you tend to concentrate on
what you *do* with o rather than the exact type of o (which is part of
the "how").

But what I do with o.m() actually depends on what o is !

Arne
 

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

Similar Threads


Back
Top