|
I'm not sure I'm all that convinced by the brittleness of code argument. I can see that it would help slightly as all local scope variables returning off a refactored function would not have to be changed. let's all lobby for a typedef keyword
|
|
|
|
|
Don't use var in a program that doesn't otherwise rely on features that were introduced in .net 3.0 (e.g. Anonymous Types).
A .net program should compile on as many versions of the framework as practical. I'm not saying you shouldn't use Generics, Anonymous Types, or Extension Methods; if you use them, that's fine. But var is really just an enabling technology; it enables the use of Anonymous Types. If you're not using Anonymous Types, then you don't need var .
If you write an Awesome Class and publish it here, someone may want to use it, that's the whole point of publishing. If that person is limited to .net 2.0 and your code contains var , but no other .net 3.x and C# 3.0 features, he will have to edit your code to replace all those needless var s. The same goes for Extension Methods; use them sparingly.
The main point here is to consider future users of the code. You yourself may change jobs and be limited in what versions you can use.
Make the most with the least.
modified on Tuesday, August 18, 2009 1:02 PM
|
|
|
|
|
...as long as you use VS2008 with .NET 2.0 targetting.
Dale Thompson
|
|
|
|
|
Yes, but not C# 2.0 -- var is a feature of C#.
|
|
|
|
|
No doubt those who are comfortable with functional languages such as F# and Scala, with their pervasive type inference, are right at home with var in C#.
Kevin
|
|
|
|
|
who was voted for that, i didnt know an CCtrlList is it a modificated version of an CListCtrl?
do i need some updates?
|
|
|
|
|
They should have written:
var ctrl=new CListCtrl();
Marc
Will work for food.
Interacx
I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner
|
|
|
|
|
It turns out to be very useful with LINQ - which is the only place I use it.
The ambiguity issues about the type (i.e., readability) are reasonable concerns. My consolation, when I use it, is that it's existence is only for LINQ, and only real declarations are made elsewhere.
Alas - one further cavaet: I rarely use C# to begin with, as C++ is lingua franca of the discerning programmer.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"It's a sad state of affairs, indeed, when you start reading my tag lines for some sort of enlightenment. Sadder still, if that's where you need to find it." - Balboos HaGadol
|
|
|
|
|
I was violently opposed to var for a long time, for all the reasons cited by other people. I even turned off the feature in Resharper. Then, I coded this for about the zillionth time:
MyCollection<ViewType, DomainType> c = new MyCollection<ViewType, DomainType>
And it struck me--I was entering the type specification twice. So, I started using var for declarations where the type was obvious.
The world didn't end, and my code was no more difficult to read. So, on a whim, I tried using it wherever possible. Again the world didn't end, and I've grown rather fond of it. I turned the feature back on in Resharper, and I use it whenever possible these days.
David Veeneman
www.veeneman.com
|
|
|
|
|
or... use vb.net and only have to type that once.
Oh, I know... flame away!
Proudly drinking the finest Maryland craft beer. Visiting Maryland for business? First round is on me!
|
|
|
|
|
Consider yourself flamed.
David Veeneman
www.veeneman.com
|
|
|
|
|
Twice? Once you type new , Intellisense writes the second one for you (says the guy who mostly uses Edit rather than VS).
And if you use that type of collection frequently, just make a type of it:
DomainViewCollection : MyCollection<ViewType, DomainType> {}
and things will be even clearer.
|
|
|
|
|
I agree totally, why type or read the type twice? Once is enough, the second one is just noise.
(In the case of var, the first one is the noise )
All the people who say var makes the code unreadable, should use R#. R# intellisense and goto's makes all vars easy to understand again!
|
|
|
|
|
I agree, and what did you saved? Half a second? A second?
I think that time is payment enough for increased readability. Try other guy interpreting your code, and you will see.
|
|
|
|
|
I use C# as my primary programming language but have never heard of var .
According to some comments here, it seems it would be a pretty popular option
|
|
|
|
|
Yes.. It is ..
If you have used anonymous types ever, you would have come across with this ...
|
|
|
|
|
it's a lot more difficult to read the code.
Regards,
unruledboy_at_gmail_dot_com
http://www.xnlab.com
|
|
|
|
|
+5 it is... and it looks like javascript...C# code looks best without var .
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Unruled Boy wrote: it's a lot more difficult to read the code.
More difficult to read or understand?
IJunk awesomeJunk = new AwesomeJunk ();
vs
var awesomeJunk = new AwesomeJunk ();
I really don't see much of a difference..?
Todd Smith
|
|
|
|
|
Those aren't equivalent are they?
|
|
|
|
|
PIEBALDconsult wrote: Those aren't equivalent are they?
yes....
Todd Smith
|
|
|
|
|
But in the first one it says IJunk, not AwesomeJunk; won't var make it AwesomeJunk instead?
|
|
|
|
|
This is typically the case when you won't use var because you want to use your AwesomeJunk as an IJunk but var will make it an AwesomeJunk.
|
|
|
|
|
Gtrwld wrote: This is typically the case when you won't use var because you want to use your AwesomeJunk as an IJunk but var will make it an AwesomeJunk.
So you just cast it if you need an IJunk.
The assumption here is public class AwesomeJunk : IJunk
It's very handy when writing things like linq extension methods.
Todd Smith
|
|
|
|
|
I agree, this is an addition to the language similar to goto. It is only meant for people which hack small programs on their own in small office environments. In real environments I do not see why I would use it, but I am open to good suggestions
One of the arguments I read in favor of using var was that code was easier to reuse ... Use generics instead !
|
|
|
|