Thursday, May 12, 2005

Creation statements

A minor but useful enhancement: take a look to this common code written in C#:
Dictionary<int,string> d =
new Dictionary<int,string>();
Why do we have to write the type name twice? Well, put the blame in polymorphism. In our example, the declared type of the local variable and the type of the created instance are the same. But we could have specified a class descending from Dictionary, if any, in the right side expression. Easy, huh?
However, most of the times both types will be the same. Could we avoid the duplication? If you're using Freya, the answer is YES:
var
d: Dictionary[Integer,String];
begin
new d;
// Now, a parameterized constructor:
new d(1024);
end;
A useful time saver, isn't it?

Updated: We have dropped the new statement after the publication of the first draft for C# 3.0. Now we have variable declarations with implicit types, as in the C# 3.0 proposal.

Labels:

0 Comments:

Post a Comment

<< Home