C# 4 adds some great features to what is already this language geek's favorite language. One of the overarching design goals of C# 4 is to be more dynamic. From the docs:
The major theme for C# 4.0 is dynamic programming. Increasingly, objects are “dynamic” in the sense that their structure and behavior is not captured by a static type, or at least not one that the compiler knows about when compiling your program.
Lets take a quick look at C# 4's
dynamic keyword, which instructs the compiler to defer binding until runtime.
Before dynamic (using reflection):
object randomizer = GetRandomizer();
Type rtype = randomizer.getType();
object randObj = rtype.InvokeMember("NextInt", BindingFlags.InvokeMethod, null, new object [] {});
int random = Convert.ToInt32(randObj);
With dynamic:
dynamic randomizer = GetRandomizer();
int random = randomizer.NextInt();
Is this just syntactic sugar? Not exactly. Its syntatic sugar plus dynamic IL generation and caching in the CLR. It gives you the flexibility of a dynamically typed scripty language but with much better performance characteristics. Running inside the CLR is already a huge advantage because, like the JVM, its a highly optimized environment with JIT / adaptive compilation and a sophisticated generational garbage collector. Most popular scripting languages simply don't enjoy as powerful a runtime environment.
Question: Doesn't this destroy the type safety and other advantages of static typing? As my doctor once responded to the inquiry, "It hurts then I do this. What should I do?" Doctor: "Don't do that." If you are writing code for a pacemaker, don't use it. There are many situations where more agile language features are useful.
Optional Parameters and Named Arguments
I have been begging for this language feature in Java and C# for years. Java's standard library is easily bloated 20%+ by all the overloaded variants of constructors and utility methods. The Swing library is a perfect example. JOptionPane has
seven constructors, each adding an additional parameter. With optional parameters and named arguments it would require one.
A simple constructor using an optional param:
Button(string text, Border border = Borders.DefaultBorder) {
this.text = text;
this.border = border;
}
var greetButton = new Button("Aloha");
I can specify the border, but if I don't, DefaultBorder will be used. This approach makes APIs and the code that uses them much terser and easier to read.
Invoking a constructor with named arguments:
var label = new Label("Address", Icon: myIcon, Opaque: true);
Nice, eh?
Generic Type Variance
Type variance may not be as exciting, but it really tightens the language. It allows, for example, the assignment of an object with the type IEnumerable<string> to a variable with the type IEnumerable<object>. Interfaces can have their type params marked as covariant or contravariant. With this addition C# has, in my opinion, the best system for handling generic types. Unlike Java, all of these concepts are enforced both by the compiler and the runtime.
Thats it for now. In my next post I'll explore how C# 4's new language features interop with the DLR (Dynamic Language Runtime.) If you want more details about C# 4, take a look at Microsoft's
New Features in C# 4.0.
Ikayzo - Design • Build • Localize | Web • Desktop • Mobile
You need to be a member of TechHui to add comments!
Join this Ning Network