var
-
So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with:
var foo = new List();
as an example, because it's obvious what foo is. What I really hate is something like this:var foo = factory.CreateAFoo()
That's where I despise seeing a "var"! MarcI'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner
It still bugs me that you can't write "new List" in C#, you have to write "new List()" But maybe I'm just a crusty old C++ guy. ;P
--Mike-- Dunder-Mifflin, this is Pam
-
It still bugs me that you can't write "new List" in C#, you have to write "new List()" But maybe I'm just a crusty old C++ guy. ;P
--Mike-- Dunder-Mifflin, this is Pam
The other day, I was staring at the screen nodding off, and I typed "var ls = List", and it looked so right. I ascended and send a messenger down to the C# 5 team. Just wait and see. :)
I have been trying for weeks to get this little site indexed. If you wonder what it is, or would like some informal accommodation for the 2010 World Cup, please click on this link for Rhino Cottages.
-
So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with:
var foo = new List();
as an example, because it's obvious what foo is. What I really hate is something like this:var foo = factory.CreateAFoo()
That's where I despise seeing a "var"! MarcI'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner
Only use it where it's needed.
Marc Clifton wrote:
it's obvious what foo is
Then don't use
var
.Marc Clifton wrote:
That's where I despise seeing a "var"!
And yet it makes a little more sense there.
-
So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with:
var foo = new List();
as an example, because it's obvious what foo is. What I really hate is something like this:var foo = factory.CreateAFoo()
That's where I despise seeing a "var"! MarcI'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner
Marc Clifton wrote:
var foo = new List();
This is actually more typing than
List foo = new List();
so there it would be kinda useless too.
Wout
-
So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with:
var foo = new List();
as an example, because it's obvious what foo is. What I really hate is something like this:var foo = factory.CreateAFoo()
That's where I despise seeing a "var"! MarcI'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner
Just make all undeclared variables vars and do away with the keyword altogether. ;)
-
So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with:
var foo = new List();
as an example, because it's obvious what foo is. What I really hate is something like this:var foo = factory.CreateAFoo()
That's where I despise seeing a "var"! MarcI'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner
Marc Clifton wrote:
var foo = factory.CreateAFoo() That's where I despise seeing a "var"!
That's where I despise seeing factories... ;) Think about it:
new List()
obviously creates an instance ofList
, hence the utility ofvar
. You'd expect CreateAFoo() to create an instance of something namedFoo
, thereby preserving the utility ofvar
- since the author instead chose to return a list without indicating this anywhere in the method name, you're trapped, trapped like a rat, between the choice to write code that is verbose and code that is unclear. The problem hardly begins withvar
either; your code becomes similarly opaque if you pass the result of the Create... call directly as a parameter to another method. -
Just make all undeclared variables vars and do away with the keyword altogether. ;)
or use some other language, such as PHP. More freedom, more joy. :laugh:
Luc Pattyn
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
Local announcement (Antwerp region): Lange Wapper? Neen!
-
It still bugs me that you can't write "new List" in C#, you have to write "new List()" But maybe I'm just a crusty old C++ guy. ;P
--Mike-- Dunder-Mifflin, this is Pam
Does the 'crusty old' relate to you or C++? ;P Use parentheses to disambiguate, young man - either (crusty old) (C++ guy) or (((crusty old) C++) guy)!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Marc Clifton wrote:
var foo = factory.CreateAFoo()
Meh, in your example
foo
is eitherFoo
orIFoo
. On a slightly related note: why on earth C# (or Java) need keywordnew
in the first place? It is completely redundant.Nemanja Trifunovic wrote:
On a slightly related note: why on earth C# (or Java) need keyword new in the first place? It is completely redundant.
So objects can be null. So you can control where an object is declared ( as in, if they are a member, etc ), and also control when you pay the cost of creating them.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with:
var foo = new List();
as an example, because it's obvious what foo is. What I really hate is something like this:var foo = factory.CreateAFoo()
That's where I despise seeing a "var"! MarcI'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner
What if
CreateAFoo()
returns something ridiculous likeIEnumerable<Dictionary<int, Dictionary<int, List<int>>>>
?Adam Maras | Software Developer Microsoft Certified Professional Developer
-
Nemanja Trifunovic wrote:
On a slightly related note: why on earth C# (or Java) need keyword new in the first place? It is completely redundant.
So objects can be null. So you can control where an object is declared ( as in, if they are a member, etc ), and also control when you pay the cost of creating them.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
I kind of like the "new" keyword, but technically it shouldn't be needed, unless I'm missing something. Just playing devil's advocate here...
With: List<string> myList = new List<string>();
Without: List<string> myList = List<string>();The parentheses would be enough to indicate that you're calling a constructor... I do think, though, that the "new" keyword keeps things clearer. There could be issues with functions named the same as classes, but that could technically be resolved with absolute references.
Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)
-
So, there's been a lot of posts about whether var (C# thingy, for those non-C# folks) is good, bad, or just ugly. Well, I can deal with:
var foo = new List();
as an example, because it's obvious what foo is. What I really hate is something like this:var foo = factory.CreateAFoo()
That's where I despise seeing a "var"! MarcI'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner
Marc Clifton wrote:
var foo = factory.CreateAFoo() That's where I despise seeing a "var"!
So what you have here is
var foo = factory.CreateAPoo();
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
What if
CreateAFoo()
returns something ridiculous likeIEnumerable<Dictionary<int, Dictionary<int, List<int>>>>
?Adam Maras | Software Developer Microsoft Certified Professional Developer
public class MyRidiculousClass : IEnumerable<Dictionary<int, Dictionary<int, List<int>>>>
{
}Now
CreateAFoo()
can return something legible :)Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)
-
Marc Clifton wrote:
var foo = new List();
This is actually more typing than
List foo = new List();
so there it would be kinda useless too.
Wout
var
wasn't created to reduce keystrokes, and should not be used as such. Developers should strive to type more keystrokes, not fewer. -
I kind of like the "new" keyword, but technically it shouldn't be needed, unless I'm missing something. Just playing devil's advocate here...
With: List<string> myList = new List<string>();
Without: List<string> myList = List<string>();The parentheses would be enough to indicate that you're calling a constructor... I do think, though, that the "new" keyword keeps things clearer. There could be issues with functions named the same as classes, but that could technically be resolved with absolute references.
Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)
OK, I guess that would work. If the method was not generic, what if you had an object called List, AND a method called List in scope that returns a List ?
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
var
wasn't created to reduce keystrokes, and should not be used as such. Developers should strive to type more keystrokes, not fewer.Huh? Isn't the point of var and anonymous types less typing? Otherwise one would type out all these types explicitly. Other than amount of typing I see no advantage in var.
Wout
-
Marc Clifton wrote:
var foo = new List();
This is actually more typing than
List foo = new List();
so there it would be kinda useless too.
Wout
Parding? I've looked and looked at your post and according to my count your code has one more letter than Marc's. So how can his be more typing than yourn?
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
OK, I guess that would work. If the method was not generic, what if you had an object called List, AND a method called List in scope that returns a List ?
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Then there would have to be a standard rule to handle it, the same way the compiler handles ambiguous methods... Local gets priority over external, same assembly gets priority over referenced assembly... Anything that can't be resolved throws an error and forces you to prefix the class or namespace. (But again, just looking at the other side here - I think
new
is a good thing)Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)
-
Parding? I've looked and looked at your post and according to my count your code has one more letter than Marc's. So how can his be more typing than yourn?
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Well, at least in VS it is... as soon as you type "new " the autocompletion will happen. So you'll type "List" just once, whereas when using var you have to type the extra var.
Wout
-
Well, at least in VS it is... as soon as you type "new " the autocompletion will happen. So you'll type "List" just once, whereas when using var you have to type the extra var.
Wout
OK. Now I understand. :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”