Tuesday, May 09, 2006
Evolution of C#
var obj = new SomeClass( p1, ..., pn) ;
instead of the redundant
SomeClass obj = new SomeClass( p1, ..., pn ) ;
Of course, we would always need the latter syntax to use polymorphism, but that's not the point. I don't like the idea because it's not removing the useless "new" part. It would be much better to borrow from C++ and use the declaration style for automatic objects:
SomeClass obj( p1, ..., pn ) ;
No redundancy, a clean syntax for anonymous objects:
SomeClass( p1, ..., pn )
and if you need polymorphism, just combine the two ideas:
SomeInterface obj = SomeClass( p1, ..., pn ) ;
Indeed, as I said in another post, "new" is totally useless in C#, as type declaration, not variable declaration, determines if the object goes on the stack or on the heap. It's actually worse than useless: it's making code unnecessarily verbose and more complex to read.
The syntax above would allow a clean language upon which we can build our own language by defining new classes; "var" is a kludge and does not significantly improve the language. (there is a role for something like "var" when using C++ templates, but that's an entirely different story, given the limited expressive power of .NET generics).




