Monday, August 30, 2010

DEV207 What's new in C# and VB.NET 4.0

Speaker: Ivan T

Tech.Ed Online

C# 4.0

Generic co- and contra-variance

Covariance
Contravariance

IEnumerable<t> is safe as only enumerating members. Won't be adding other subtype to collection.

IComparer<t>
-

If T appears onlay as an output, it's safe to pass X<tderived> for X<t>
- covariance
- C# syntax X<out>

If T appears only as an input, it's safe to pass X for X
- countravariance
- C# syntax X<int>

List and IList are not supported but IComparer and IEnumerable do support.

Optional and named parameters
- Optional
public static virtual DoStuff(int first, int second = 123, int third = 456)
{
}

DoStuff(888);

Not creating an Overload.
Default value is stored in the calling assembly. Something to consider when versioning the libraries. Not for external publicly visible methods.

DoStuff(888, third: 999);

Dynamic Programming
Dynamic Language Runtime

COM interop
"No PIA" Primary Interop Assembly
- Merge COM interop type definitions into the calling assembly.
- Reduces total project size.

COM interop  enhancements
- Can now leave out unnecessary arguments, auto fill in "ref missing" arguments and identify parameters by name.
- Dynamic helps with weak typing problem.

Tuple
- Simple way of packaging up data into a single object without creating the actual implementation. Not meaningful property names.
Tuple.Create(true, "bob");
Imutable