Alex Rivera | Logout

New Cool Features of C# 4.0

Asked 2008-11-15T07:19:57.500
28

What are the coolest new features that you guys are looking for, or that you've heard are releasing in c# 4.0.

Edit
Report

3 Answers

9

IDynamicObject, the glue behind dynamic, allows interpretation of a call at runtime.

This is interesting for inherently untyped scenarios such as REST, XML, COM, DataSet, dynamic languages, and many others. It is an implementation of dynamic dispatch built on top of the Dynamic Language Runtime (DLR).

Instead of cumbersome reflection semantics, you dot into variables declared as dynamic. Imagine working with Javascript objects from Silverlight:

dynamic obj = GetScriptObject();

HtmlPage.Window.Alert(obj.someProperty);

All C# syntax is supported (I believe):

HtmlPage.Window.Alert(obj.someMethod() + obj.items[0]);

Reflection itself looks a lot cleaner:

public void WriteSomePropertyValue(object target)
{
    Console.WriteLine((target as dynamic).SomeProperty);
}

public void WriteSomeMethodValue(object target, int arg1, string arg2)
{
    Console.WriteLine((target as dynamic).SomeMethod(arg1, arg2));
}

dynamic is another tool in the toolkit. It does not change the static vs. dynamic debate, it simply eases the friction.

answered 2008-11-15T08:16:26.437
3
  • C# 2.0 —Generics (.NET Framework support was added, and C# benefited from this); iterator pattern (the yield keyword); anonymous methods (the delegate keyword), nullable types, and the null coalescing operator (??).
  • C# 3.0 —Anonymous types, extension methods, object initializers, collection initializers, implicitly typed local variables (var keyword), lambda expressions (=>), and the LINQ query expression pattern.
  • C# 4.0 —Optional Parameters and Named Arguments, Dynamic typing (dynamic type), improved COM-Interop, and Contra and Co-Variance.
answered 2010-11-04T09:32:56.187
2

C#4.0

1) PLinq

2) Named and Optional Parameters

3) Lazy

4) Co & Contra Variance

5) Task Parallel

6) Dynamic object

7) Expando object

8) Improved COM-Interop

9)Tuple

only to name a few

answered 2010-11-09T02:51:43.237

Your Answer