var
-
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.
-
var
wasn't created to reduce keystrokes, and should not be used as such. Developers should strive to type more keystrokes, not fewer. -
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.
It only saves some typing, other than that it doesn't offer anything functional. I can make my own custom classes to contain my query/filter results, which will always be more readable than a non-descriptive var.
Wout
-
My opinion: I'm kind of liking var. Everywhere. While I might not be able to tell the type at only a quick glance, as you say in your post, it makes the code much cleaner and elegant, IMO, as the variable declarations all stand out as a single group.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango
Why not just make all your own custom types 7 characters long; and build wrappers around the base classes to make them 7 characters long too?
QString bork = string.Empty
QQFloat FHeight = 12.0FReally, what I normally do is group the declarations at the top of a class inside a
#region Declarations
then shrink it when not needed.
Ninja (the Nerd)
Confused? You will be... -
Yes.
Wout
UnstableClassInDevelopment.UnStableType.UnderlyingValue obj = new UnstableClassInDevelopment.UnStableType.UnderlyingValue();
Now, if that underlying value changes type; everything breaks. With var, you don't have to do any search/replace and it works with any reasonable change. Yes, stupid example but I don't even use the thing.
Ninja (the Nerd)
Confused? You will be... -
UnstableClassInDevelopment.UnStableType.UnderlyingValue obj = new UnstableClassInDevelopment.UnStableType.UnderlyingValue();
Now, if that underlying value changes type; everything breaks. With var, you don't have to do any search/replace and it works with any reasonable change. Yes, stupid example but I don't even use the thing.
Ninja (the Nerd)
Confused? You will be...You can just rename the type with VS refactoring couldn't you? And if some third party owns that code and changes it, I would like everything to break, so I can see what the change was exactly. Silently accepting changes is very dangerous.
Wout
-
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)
var foo = new MyFoo(); //clearly creates a new MyFoo. var foo = MyFoo(); //create a new MyFoo or call local method MyFoo()? var foo = MyFoo; //create a new MyFoo or assign local field MyFoo?
Truth, James
-
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.
I pity the foo! - Mr <T>
"The activity of 'debugging', or removing bugs from a program, ends when people get tired of doing it, not when the bugs are removed." - "Datamation", January 15, 1984
-
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.>> why on earth C# (or Java) need keyword new in the first place? It is completely redundant. It's not for the compiler, but for human readers. The goal was that the programmer's intend is always clear. For more examples, there's no reason why classes need to be marked abstract; or that virtual methods be marked "new" or "override"; or that we have both "ref" and "out" to mark parameters. none of those affects the generated IL at all.
Truth, James
-
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
I write
new List()
even in C++. The constructor is a function, even if it's implicit.If you can play The Dance of Eternity (Dream Theater), then we shall make a band.
-
I pity the foo! - Mr <T>
"The activity of 'debugging', or removing bugs from a program, ends when people get tired of doing it, not when the bugs are removed." - "Datamation", January 15, 1984
I pity the foo! Now eat my cereal - Pee Wee Herman acting like Mr