473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

question on anonymous type

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
Jun 27 '08 #1
22 1343
On Jun 3, 11:03 am, timor.su...@gmail.com wrote:
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).
Jun 27 '08 #2
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.
Jun 27 '08 #3
On Jun 3, 11:03 am, timor.su...@gmail.com wrote:
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
Jun 27 '08 #4
Thanks all for your answer.

That's mean that in IL language, both instructions are equivalent ?
On 3 juin, 12:25, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 3, 11:03 am, timor.su...@gmail.com wrote:
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
Jun 27 '08 #5
On Jun 3, 1:10 pm, timor.su...@gmail.com wrote:
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
Jun 27 '08 #6
Ok, thanks for your answer

Best regards

On 3 juin, 14:27, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 3, 1:10 pm, timor.su...@gmail.com wrote:
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
Jun 27 '08 #7
On Jun 3, 6:03*am, timor.su...@gmail.com wrote:
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
Jun 27 '08 #8
*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
Jun 27 '08 #9
ti*********@gmail.com wrote:
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
Jun 27 '08 #10
On Jun 5, 3:34 am, Arne Vajhøj <a...@vajhoej.dkwrote:
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.
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
Jun 27 '08 #11
Jon Skeet [C# MVP] wrote:
On Jun 5, 3:34 am, Arne Vajhøj <a...@vajhoej.dkwrote:
>>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.

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".
>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.
I can not see that redundancy as a big problem. The compiler
will catch it if inconsistent changes are made.

Arne
Jun 27 '08 #12
Arne Vajhøj <ar**@vajhoej.dkwrote:
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".
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.
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.
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 Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #13
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
>>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".

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.
>>>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.
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.
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
Jun 27 '08 #14
Arne Vajhøj wrote:
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".
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.

--

Jun 27 '08 #15
Arne Vajhøj <ar**@vajhoej.dkwrote:
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.
I'm sure they'll survive - they just may not be as productive.
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.
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

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #16
Tim Jarvis wrote:
Arne Vajhøj wrote:
>>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".

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
Jun 27 '08 #17
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
>>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.

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.
>>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.

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

Who was arguing that disk space was relevant?
You said that it takes more space.
>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
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

Jun 27 '08 #18
Arne Vajhøj <ar**@vajhoej.dkwrote:
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.
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.
>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.
In many other scenarios, yes. Not here.
Disk space is cheap.
Who was arguing that disk space was relevant?
You said that it takes more space.
I was talking about *visual* space. *That* is important - disk space
(for source code) generally isn't.
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
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 Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #19
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
>>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.

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#.
>>>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
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").
But what I do with o.m() actually depends on what o is !

Arne
Jun 27 '08 #20
Arne Vajhøj <ar**@vajhoej.dkwrote:
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#.
In which case it would be interesting to see whether it becomes more
requested in a few years' time, when those developers have been using
C# 3 and become used to "var". It's too early to take results *now* and
draw any conclusions.
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 !
But if it's obvious when *reading* the code what's going on, the
details only matter when you're looking more deeply into the code. When
I'm working on a problem, I don't actually need to read most of the
code closely - I'll skim a lot of it. Implicitly typed local variables
help the skimming mode, and arguably slightly harm the detailed part.
(Usually it's still pretty obvious what the type actually is, IMO.)

The aid to the skimming mode is significant though.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #21
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
>>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#.

In which case it would be interesting to see whether it becomes more
requested in a few years' time, when those developers have been using
C# 3 and become used to "var". It's too early to take results *now* and
draw any conclusions.
True. It could change.

But I doubt it will happen. It is not exactly a new concept.

I don't even think C# would have gotten it if it had not been for LINQ.
>>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 !

But if it's obvious when *reading* the code what's going on, the
details only matter when you're looking more deeply into the code.
I don't consider the type a detail in OOP.
When
I'm working on a problem, I don't actually need to read most of the
code closely - I'll skim a lot of it. Implicitly typed local variables
help the skimming mode, and arguably slightly harm the detailed part.
I believe many people will want to know the type even for skimming.

Arne
Jun 27 '08 #22
Arne Vajhøj <ar**@vajhoej.dkwrote:
In which case it would be interesting to see whether it becomes more
requested in a few years' time, when those developers have been using
C# 3 and become used to "var". It's too early to take results *now* and
draw any conclusions.
True. It could change.

But I doubt it will happen. It is not exactly a new concept.
It's not a new *concept* but it may be new in *practice* to a lot of
people. Heck, I'd never used a lambda expression in anger until C# 3,
and those have been around for ages too.
I don't even think C# would have gotten it if it had not been for LINQ.
Quite possibly not - but that doesn't mean the feature isn't handy
outside LINQ. Extension methods could be thought of in the same way,
and those *are* on the cards for Java 7.
But if it's obvious when *reading* the code what's going on, the
details only matter when you're looking more deeply into the code.
I don't consider the type a detail in OOP.
I do when it's a matter of skimming code. Do I care whether I'm using
an array or a List<Tif I'm just indexing it? Not really. Do I care
whether something is definitely a LinkedList<Tor any old
IEnumerable<Tif all I'm doing is iterating over it? Not really.

Bear in mind that the declared variable type won't tell you what the
actual type is anyway (unless it's a sealed type) - if you need that
information you've *got* to look to the right hand side of the
assignment. If you're going to do that, how much do you care about the
LHS?

I'm not saying it isn't important information - just that I don't find
it important in certain modes of reading code, and those modes account
for quite a lot of the time I may spend reading code (either for
debugging and trying to get to the critical spot quickly, then
understanding that critical spot in detail, or just getting the flavour
of a class).

But hey, it's definitely a style thing. If you don't like it, that's
fine.
I'm working on a problem, I don't actually need to read most of the
code closely - I'll skim a lot of it. Implicitly typed local variables
help the skimming mode, and arguably slightly harm the detailed part.
I believe many people will want to know the type even for skimming.
Then I believe they won't skim as quickly as they can, for the basic
level of "what's this method trying to achieve?" that I aim for with
skimming.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Cho | last post by:
Class Cartesian { double x; double y; public: Cartesian( ) { x = 0.0 ; y = 0.0;} Cartesian(double x1, double y1) { x = x1; y = y1;} }; class Polar { double radius;
2
by: joltman | last post by:
OK, this is kind of hard to explain, so I'll do my best: I have a form where I have a row where there could be multiple entries, so I have a link where it will dynamically add another row like it,...
6
by: amethyste | last post by:
hello, This is my sample (simplified): public class Binom { public string _Value;
4
by: TR | last post by:
MyPage.htm has <FORM ACTION="Register.aspx" METHOD="POST">. Where does Register.aspx reference the values passed to it via the Post method in MyPage.htm? Thanks in advance. TR
20
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
4
by: Harold Howe | last post by:
I am running into a situation where the compiler complains that it cannot infer the type parameters of a generic method when one of the function arguments is an anonymous method. Here is a...
0
by: ashish | last post by:
hi all, I am working with a custom profile provider that stores my business object as profile with anonymous identification turned on. I have a question regarding how it is supposed to work...
7
by: Daniel Jeffrey | last post by:
Hello, I am coming from Delphi and when I have a method in the inherited class, I call Inherited to call the base class method. This is handy cause you can control where and when these things...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.