473,396 Members | 1,961 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,396 software developers and data experts.

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

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
59 3135

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

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

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

Similar topics

2
by: Russell Reagan | last post by:
In a newer version of a chess program I am writing, I have created classes that are (more or less) drop in replacements for things that used to be plain old integer or enumerated variables (colors,...
9
by: Simon | last post by:
Hi All, Is it possible to disallow implicit casting for an operand of a function written in C? i.e. void foo(int a) {..} short b; foo(b) // error without explicit cast
25
by: Chad Z. Hower aka Kudzu | last post by:
I have defined some implicit convertors so that I can do: MyStruct x = 4; The implicit convert the 4 into MyStruct. But its essentially a copy constructor and thus if I had: MyStruct x;...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.