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
Agreed... Only two places I'll use var... 1) In initializations like you described, when the type is right there anyway 2) Linq anonymous types
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
you have a point. I've taken to only using
var
where the type is obvious. -
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()
into every language a little void * must fall.
-
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.Because Bill (Jim's mate) keep calling instances capitalized... :rolleyes:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
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. ;)
-
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!
-
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. -
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