By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,405 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,405 IT Pros & Developers. It's quick & easy.

implicit types in C# 3.0, what is the usefulness of them?

P: n/a
The C# 3.0 spec (http://msdn.microsoft.com/vcsharp/future/) contains a
feature called "Implicitly typed local variables".

The type of the variable is determined at compile time based on the
expression to the right of the = sign:

var s = "Hello"; //s is strongly typed to be a string
var i = 25; //i is strongly typed to be an integer

But what I don't understand what advantage it is?

string s = "Hello"; //This is just as easy to type and is easier to
read

What's the story?

Nov 17 '05
Share this Question
Share on Google+
59 Replies


P: n/a

> A lot of this is typing as I think, I'll readily admit - it's as much a
> gut instinct as anything else. I really just don't like not being able
> to see a variable's type immediately from its declaration.


Then why use aliases, which does not allow that, period. You have to look
up
the type still.


Only if the type name doesn't give enough information, which I believe
it could. To be honest, I probably wouldn't actually do that - I'd
probably just stick with the long name in both places.

Only time will tell, but I'm going to stay wary of the whole business
for a while...


Ya, its going to take a while to adjust to, not to mention some good "best
practicies" and possibly some warnings.

I really dislike alot of the var stuff. var = 6; var=this; and var
=Method(); all seem horribly incomplete. It is basically just the anonymous
type and the complex generic where I initalize it on the same line that I
like it for. I would like the IDE to offer automatic type specification on
the fly, though. Something like::

var o = new Dictionary<int,int>();

transforms into

Dictionary<int,int> o = new Dictionary<int,int>();

as soon as I hit enter or whatever. It saves typing, keeps the langauge
consistent(since limiting var's usage could be construed as an
inconsistency), and keeps code written in the IDE easy to read without
making it harder to type.
Nov 17 '05 #51

P: n/a
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:

<snip>
I really dislike alot of the var stuff. var = 6; var=this; and var
=Method(); all seem horribly incomplete. It is basically just the anonymous
type and the complex generic where I initalize it on the same line that I
like it for. I would like the IDE to offer automatic type specification on
the fly, though. Something like::

var o = new Dictionary<int,int>();

transforms into

Dictionary<int,int> o = new Dictionary<int,int>();

as soon as I hit enter or whatever. It saves typing, keeps the langauge
consistent(since limiting var's usage could be construed as an
inconsistency), and keeps code written in the IDE easy to read without
making it harder to type.


Right - now *that* I would have no problems with at all. I don't mind
IDEs speeding things up (and indeed in VS.NET I always feel very
limited compared with when I'm doing Java development in Eclipse) - so
long as the result is readable code :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #52

P: n/a
Daniel O'Connell [C# MVP] wrote:

> A lot of this is typing as I think, I'll readily admit - it's as much a >> > gut instinct as anything else. I really just don't like
not being able >> > to see a variable's type immediately from its
declaration.
Then why use aliases, which does not allow that, period. You have to look >> up the type still.


Only if the type name doesn't give enough information, which I
believe it could. To be honest, I probably wouldn't actually do
that - I'd probably just stick with the long name in both places.

Only time will tell, but I'm going to stay wary of the whole
business for a while...


Ya, its going to take a while to adjust to, not to mention some good
"best practicies" and possibly some warnings.

I really dislike alot of the var stuff. var = 6; var=this; and var
=Method(); all seem horribly incomplete. It is basically just the
anonymous type and the complex generic where I initalize it on the
same line that I like it for. I would like the IDE to offer automatic
type specification on the fly, though. Something like::

var o = new Dictionary<int,int>();

transforms into

Dictionary<int,int> o = new Dictionary<int,int>();

as soon as I hit enter or whatever. It saves typing, keeps the
langauge consistent(since limiting var's usage could be construed as
an inconsistency), and keeps code written in the IDE easy to read
without making it harder to type.


Now THAT's a wonderful idea!. I'd suggest you make a case for this
wonderful idea at the summit! :) They might find it a bit over the top
as every compilermaker always wants to avoid the point where the
compiler can actively update the sourcecode, but in this case it's
pretty OK IMHO.

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Nov 17 '05 #53

P: n/a
I really dislike alot of the var stuff. var = 6; var=this; and var
=Method(); all seem horribly incomplete. It is basically just the
anonymous type and the complex generic where I initalize it on the
same line that I like it for. I would like the IDE to offer automatic
type specification on the fly, though. Something like::

var o = new Dictionary<int,int>();

transforms into

Dictionary<int,int> o = new Dictionary<int,int>();

as soon as I hit enter or whatever. It saves typing, keeps the
langauge consistent(since limiting var's usage could be construed as
an inconsistency), and keeps code written in the IDE easy to read
without making it harder to type.


Now THAT's a wonderful idea!. I'd suggest you make a case for this
wonderful idea at the summit! :) They might find it a bit over the top


I won't be at the summit, unfortunatly. I'm back in school, so its back to
poor student mode, ;).

However, if someone else wants to take it up, feel free. It is really not
much different than the expansions the IDE already does.
Nov 17 '05 #54

P: n/a
On 2005-09-17, Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:


I would like the IDE to offer automatic type specification on
the fly, though. Something like::

var o = new Dictionary<int,int>();

transforms into

Dictionary<int,int> o = new Dictionary<int,int>();

as soon as I hit enter or whatever. It saves typing, keeps the langauge
consistent(since limiting var's usage could be construed as an
inconsistency), and keeps code written in the IDE easy to read without
making it harder to type.


But the IDE already does this for you, although it does it in the
opposite direction, which makes much more sense IMHO.

If I type

Dictionary<int,int> o = new

Intellisense will pop up with Dictionary<int,int> already selected. It
takes less typing than the var alternative.
Nov 17 '05 #55

P: n/a

"david" <da***@woofix.local.dom> wrote in message
news:sl******************@localhost.localdomain...
On 2005-09-17, Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net>
wrote:


I would like the IDE to offer automatic type specification on
the fly, though. Something like::

var o = new Dictionary<int,int>();

transforms into

Dictionary<int,int> o = new Dictionary<int,int>();

as soon as I hit enter or whatever. It saves typing, keeps the langauge
consistent(since limiting var's usage could be construed as an
inconsistency), and keeps code written in the IDE easy to read without
making it harder to type.


But the IDE already does this for you, although it does it in the
opposite direction, which makes much more sense IMHO.

If I type

Dictionary<int,int> o = new

Intellisense will pop up with Dictionary<int,int> already selected. It
takes less typing than the var alternative.


It only does it sometimes, if you use an interface or abstract class it
won't. Although it clearly is similar.
Nov 17 '05 #56

P: n/a
On 2005-09-18, Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:

"david" <da***@woofix.local.dom> wrote in message
news:sl******************@localhost.localdomain...
But the IDE already does this for you, although it does it in the
opposite direction, which makes much more sense IMHO.

If I type

Dictionary<int,int> o = new

Intellisense will pop up with Dictionary<int,int> already selected. It
takes less typing than the var alternative.


It only does it sometimes, if you use an interface or abstract class it
won't. Although it clearly is similar.


But var could never possibly work in that instance either.

var list = new List<int>();

There's no way that the IDE could know you wanted to declare an
IList here. And when it chose the wrong one, you'd be on the next
line and it would be a pain to correct it.

OTOH, I agree there's a lot of room for intellisense to improve here. If
I declare an interface, intellisense should show me classes that
implement it (I haven't played with VS2005 much, so it could be they're
better at that than they were).


Nov 17 '05 #57

P: n/a
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> schrieb im
Newsbeitrag news:%2****************@TK2MSFTNGP12.phx.gbl...

"david" <da***@woofix.local.dom> wrote in message
news:sl******************@localhost.localdomain...
On 2005-09-17, Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net>
wrote:


I would like the IDE to offer automatic type specification on
the fly, though. Something like::

var o = new Dictionary<int,int>();

transforms into

Dictionary<int,int> o = new Dictionary<int,int>();

as soon as I hit enter or whatever. It saves typing, keeps the langauge
consistent(since limiting var's usage could be construed as an
inconsistency), and keeps code written in the IDE easy to read without
making it harder to type.


But the IDE already does this for you, although it does it in the
opposite direction, which makes much more sense IMHO.

If I type

Dictionary<int,int> o = new

Intellisense will pop up with Dictionary<int,int> already selected. It
takes less typing than the var alternative.


It only does it sometimes, if you use an interface or abstract class it
won't. Although it clearly is similar.

It does it in all cases where var x = new ... would work.
Nov 17 '05 #58

P: n/a
I think what Daniel is saying is that if you wanted var to actaully be typed to an interface rather than the concrete type then the IDE can't do that for you

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> schrieb im
Newsbeitrag news:%2****************@TK2MSFTNGP12.phx.gbl...

"david" <da***@woofix.local.dom> wrote in message
news:sl******************@localhost.localdomain...
On 2005-09-17, Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net>
wrote:


I would like the IDE to offer automatic type specification on
the fly, though. Something like::

var o = new Dictionary<int,int>();

transforms into

Dictionary<int,int> o = new Dictionary<int,int>();

as soon as I hit enter or whatever. It saves typing, keeps the langauge
consistent(since limiting var's usage could be construed as an
inconsistency), and keeps code written in the IDE easy to read without
making it harder to type.


But the IDE already does this for you, although it does it in the
opposite direction, which makes much more sense IMHO.

If I type

Dictionary<int,int> o = new

Intellisense will pop up with Dictionary<int,int> already selected. It
takes less typing than the var alternative.


It only does it sometimes, if you use an interface or abstract class it
won't. Although it clearly is similar.

It does it in all cases where var x = new ... would work.

[microsoft.public.dotnet.languages.csharp]
Nov 17 '05 #59

P: n/a
For all I have read in this topic, I just didn't see any anonymous types.
The "var" keyword is just a way to delclare a variable with type inferrence.
The anonymous types are another cool stuff where you can define and
instanciate a new type (data oriented) in the same instruction. This could be
very usefull to get views on data:

var resultView = from newsItem in news,
author in authors
where newsItem.AuthorId == author.Id
select new(NewsTitle = newsItem.Title,
NewsContent = newsItem.Content, AuthorName = author.Name);

the last line create for each "row" returned by the query an instance of an
anonymous type. As this type is not named, you have no choice but use the
"var" keyword.

Sorry for my poor English...
Nov 17 '05 #60

59 Replies

This discussion thread is closed

Replies have been disabled for this discussion.