var
-
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.”
-
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.
That would be a mess...
If the post was helpful, please vote, eh! Current activities: Book: Devils by Fyodor Dostoyevsky Project: Hospital Automation, final stage Learning: Image analysis, LINQ Now and forever, defiant to the end. What is Multiple Sclerosis[^]?
-
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
No.
-
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
Stuart Dootson wrote:
Does the 'crusty old' relate to you or C++?
It works either way. :-D
Stuart Dootson wrote:
(((crusty old) C++) guy)!
Do we allow Lisp in here now? ;)
--Mike-- Dunder-Mifflin, this is Pam
-
No.
Yes.
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
Marc Clifton wrote:
var foo = factory.CreateAFoo() That's where I despise seeing a "var"!
Well if both the variable and the factory method are named properly, I don't think there'd be any confusion here. If they are not, then using var would be the least of the issues here.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
My latest book : C++/CLI in Action / Amazon.com link -
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
no, the point was more to be able to have anonymous types and also in foreach and other like constructs where the type info can be combersomely unwieldy to have something more readable and understandable.
-
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)
yes, true, but you're not always using your own "reasonable" code...
-
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)
Then you lose any constructors the base type might have had. Although it's annoying you have to fully qualify the namespaces, you could also do this:
using TooLong = System.Collections.Generic.IEnumerable<System.Collections.Generic.Dictionary<int, System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<int>>>>;
Visual Studio is an excellent GUIIDE.
-
Nemanja Trifunovic wrote:
Meh, in your example foo is either Foo or IFoo.
Agreed. The example was bad, but you know what I meant. :)
Nemanja Trifunovic wrote:
why on earth C# (or Java) need keyword new in the first place? It is completely redundant.
Because it gives everyone a warm fuzzy feeling that something important is happening. Just be glad we don't have to use the "Let" keyword (though, in some functional languages, it's baaaaack!) Marc
I'm not overthinking the problem, I just felt like I needed a small, unimportant, uninteresting rant! - Martin Hart Turner
But what if you want to reuse an object for something completely different. Can you declare var Foo2 = old FooObject; ?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
-
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)
List<string> myList = List<string>.ctor() ;
Much more orthogonal. :-D -
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
//C/C++:
void *foo = (void*)new Foo();
//What is foo?
foo = (void*)myClass.createFoo();
//And now?
foo = (void*)&any_type_variable;
//now?
foo = (void*)&foo;
//O.oSeems nice to me :P
-
But what if you want to reuse an object for something completely different. Can you declare var Foo2 = old FooObject; ?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
let var Foo2 = old FooObject; of course... (Seriously, no. The compiler substitutes the var keyword with the real type at compile time. It's not a dynamic type.)
-- Kein Mitleid Für Die Mehrheit
-
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
I hate it because you can't tell at a glance what the type is, okay so its alright for anonymous types where you don't know but otherwise it's just a train-wreck. I still think that the following is cleaner, easier to read and less prone to human error:
List foo = new List();
andIFoo foo = factory.CreateAFoo()
-
Marc Clifton wrote:
var foo = factory.CreateAFoo()
into every language a little void * must fall.
Chris Losinger wrote:
Marc Clifton wrote: var foo = factory.CreateAFoo() into every language a little void * must fall.
In the above case, foo is NOT void *. It is a strongly typed variable of the type factory.CreateAFoo() returns. For instance:
var str = "This is a string variable";
str = 5; // <-- Compile errorIncidently, I love to use var. During development you change the type of a variable by changing the initialization code and not have to go also and fix the variable declared type. So if I have in my code the above mentioned function declared as so:
internal Foo CreateAFoo()
{
// create and return a Foo
}And somewhere else:
var foo = factory.CreateAFoo()
Then, during the developement process, I think: "let's change my work from Foo to IFoo to introduce different implementations". All I have to do is change the signature of CreateAFoo() to:
internal IFoo CreateAFoo()
{
// create and return a Foo
}That's it, all done. Don't need to wory about changing all the variables all over the code that hold a Foo reference. They automatically become IFoo references. --- Adar Wesley
-
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
-
But what if you want to reuse an object for something completely different. Can you declare var Foo2 = old FooObject; ?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
Roger Wright wrote:
var Foo2 = old FooObject;
There's no foo like an old foo. So, clearly not.
_____________________________ There is no I in team. But there is meat in there.