Re: Are people abusing "var"?
- From: "Christophe Lephay" <christophe-lephay@xxxxxxxxxx>
- Date: Wed, 26 Nov 2008 18:08:18 +0100
"MC" <for.address.look@xxxxxxxxxxxxxxxxxxxxxxx> a écrit dans le message de groupe de discussion : OlGSAPzTJHA.5024@xxxxxxxxxxxxxxxxxxxxxxx
"Michael B. Trausch" <mike@xxxxxxxxxx> wrote in message news:20081125130745.613db209@xxxxxxxxxxxxxxxxxxxxxxxxxxxxOn Tue, 25 Nov 2008 15:19:24 -0000
"Mark Rae [MVP]" <mark@xxxxxxxxxxxxxxxxx> wrote:
Even though C# makes a reasonable stab at choosing the most
appropriate datatype
(http://msdn.microsoft.com/en-us/library/bb384061.aspx), I prefer to
tell it what I mean explicitly rather than allow it to decide what it
thinks I mean implicitly...
Absolutely. This is one of the reasons that I choose to use statically
typed languages at all: not using some mechanism like "var" eliminates
many classes of code by specifying _precisely_ what you want. While
"var" is easily figured out by the computer, it's far easier for a
human to see "int" or "string" or even "object", if it must be.
[ "many classes of code" => "many classes of coding errors" ? ]
Right! By declaring types, you make sure the compiler knows what YOU are thinking, so if you make a mistake later on, you'll be warned!
This was one of the key insights in the original design of Pascal (which, despite superficial differences, is an important intellectual ancestor of C#): Let the compiler help you keep >track of what YOU think the data types and structures should be.
But this is not an issue with auto.
Var doesn't mean variant, and the object is still strongly typed. If the compiler can deduce what you mean without any ambiguity, why should you still need to say it ?
Another thing to consider is that var can only be used locally. So, if the reader has a problem to understand what is the real type of the object (let's suppose the intellisens won't tell him), i guess there is something very wrong either with the function, either with the reader.
And because the object will be very local, to know preciseley what is the type of the object will be useless most of the time, as long as the compiler can do its type checking.
.
- Follow-Ups:
- Re: Are people abusing "var"?
- From: Anthony Jones
- Re: Are people abusing "var"?
- References:
- Are people abusing "var"?
- From: Jeff Johnson
- Re: Are people abusing "var"?
- From: Frank
- Re: Are people abusing "var"?
- From: Mark Rae [MVP]
- Are people abusing "var"?
- Prev by Date: Re: Web Page Controls
- Next by Date: Re: Are people abusing "var"?
- Previous by thread: Re: Are people abusing "var"?
- Next by thread: Re: Are people abusing "var"?
- Index(es):
Relevant Pages
|