28

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

bendewey
  • 39,709
  • 13
  • 100
  • 125

10 Answers10

29

The dynamic stuff sounds cool if you need it but I don't expect to use it very often.

The generic variance for delegates and interfaces is similar - the lack of variance is a headache at the moment, but many of the places where it's a pain won't be covered by the limited variance available in C# 4.

The COM features don't particularly interest me - I really ought to get more of a handle on what they are though.

Optional and named parameters could make a big difference when building immutable types: it enables syntax like:

Person p = new Person (name: "Jon", age: 32);

without having mammoth combinations of constructor overloads. I'd prefer a bit more support for writing immutable types in the form of readonly automatically implemented properties, but I don't expect we'll get those. (They certainly aren't in the proposed feature list at the moment.)

I'm personally actually more interested in a couple of the framework features of .NET 4.0 - in particular code contracts and parallel extensions.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    To add an example of this, see: http://marcgravell.blogspot.com/2008/11/immutability-and-optional-parameters.html – Marc Gravell Nov 15 '08 at 09:08
  • 1
    This video is informative if you have an hour to kill... http://channel9.msdn.com/pdc2008/TL16/ – user10178 Nov 15 '08 at 13:19
  • 2
    Named parameters also make boolean arguments much, much clearer. Would you like to see 'True' or 'UseWidget: True'? – Craig Gidney Feb 17 '10 at 01:53
  • 1
    I think the idiomatic example of needing "the dynamic stuff" is COM interop, even though it breaks my preferred method of API discovery with IntelliSense. – György Andrasek May 16 '10 at 14:34
  • 1
    @Jurily: Not everything about dynamic interop needs to break IntelliSense. You can use implicit conversions from dynamic but specify the target type, and then still have IntelliSense on the result. It's a compromise solution, basically. – Jon Skeet May 16 '10 at 17:08
17

Method parameter default values:

public void MyMethod1(string value1 = "test", int num1 = 10, double num2 = 12.2)
{
  //...
}

Also maybe anonymous return types:

public var MyMethod2()
{
  // ..
}
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
9

Tuples

James L
  • 16,456
  • 10
  • 53
  • 70
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.

Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
  • 4
    Holy crap.... Hadn't thought about casting objects to dynamic. That DOES make reflection a crap-ton easier! –  Sep 15 '09 at 20:58
3

Enhanced support for Expression Trees!

Chris Shouts
  • 5,377
  • 2
  • 29
  • 40
3

Not strictly C#, but since .NET is tagged here's a good link regarding BCL changes.

Note to self: Must rename my Stopwatch.Restart() extension method before 4.0 is released :)

si618
  • 16,580
  • 12
  • 67
  • 84
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.
Amir Rezaei
  • 4,948
  • 6
  • 33
  • 49
2

the dynamic keyword looks like it can bridge the gap between dynamic languages like IronRuby or IronPython quite nicely, which will probably help its adoption in the Microsoft monoculture... that excites me.

While I'm intrigued by it, I'm also worried that it will be overused, like Generics and LINQ, SQLCLR, etc :)

just somebody
  • 18,602
  • 6
  • 51
  • 60
Ben Scheirman
  • 40,531
  • 21
  • 102
  • 137
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

0

Ability to write asynchronous code in synchronous fashion with async and await is cool.

Embedd_0913
  • 16,125
  • 37
  • 97
  • 135