.NET 2.0 -> 4.0 What's new

Bas Bossink

August 2011

Contents

Introduction

Time table

Time VS Framework C# Theme
2005 2005 2.0 2.0 Generics
2006 2005 3.0 2.0 WPF, WCF, WF
2008 2008 3.5 3.0 LINQ
2009 2008 SP1 3.5 SP1 3.0
2010 2010 4.0 4.0 Dynamic, Parallel

C# Features (3.0)

C# Features (4.0)

Implicitly Typed Local Variables

Dictionary<string,Func<string,double>> fred = 
    new Dictionary<string, Func<string,double>>();

 ↓ 

var fred = new Dictionary<string, Func<string,double>>();

Auto-Implemented Properties

public class Flinstone { 
    private string m_Name;
    public string Name {
        get { return m_Name; }
        set { m_Name = value; }
    }
}

 ↓ 

public class Flinstone {
    public string Name { get; set; }
}

Collection Initializers

var fred = new List<int>() { 37, 42, 53 };

Object Initializers

var fred = new Flinstone { Name = "Fred" };

Extension methods

public static string YabaDabaDo(this Flinstone fred) {
    ...
}
...
var noise = fred.YabaDabaDo();

Anonymous types

var fred = new { Name = "Fred", Friend="Barney" };

λ Expressions

var flinstones = bedrockCitizins.Find(
    delegate(string c) { 
        return c.Lastname.Equals("Flinstone"); 
    });

 ↓ 

var flinstones = bedrockCitizins.Find(
    c => c.Lastname.Equals("Flinstone"));

Query keywords (LINQ)

LINQ

Query keywords (LINQ)

  • from
  • let
  • where
  • ascending
  • select
  • descending
  • group
  • on
  • into
  • equals
  • orderby
  • in
  • join
  • by

LINQ examples

var flinstones = from citizen in bedrockCitizens
                 where citizen.Name.Equals("Flinestone")
                 select citizen.Name;

var pythagoreanTriples = from a in Enumerable.Range(1, 25)
                         from b in Enumerable.Range(a, 25 - a)
                         from c in Enumerable.Range(b, 25 - b)
                         where a * a + b * b == c * c
                         select Tuple.Create(a, b, c);

LINQ, λ Expressions, Homoiconicity

Func<int, int> twiceD = x => x * 2; 
Expression<Func<int, int>> twiceE = x => x * 2;

Partial Method Definitions

// Definition in file1.cs
partial void onNameChanged();

// Implementation in file2.cs
partial void onNameChanged()
{
  // method body
}

Dynamic

Dynamic Example

// Before the introduction of dynamic.
((Excel.Range)excelApp.Cells[1, 1]).Value2 = "Name";
Excel.Range range2008 = (Excel.Range)excelApp.Cells[1, 1];


// With dynamic 
excelApp.Cells[1, 1].Value = "Name";
Excel.Range range2010 = excelApp.Cells[1, 1];

Named and optional arguments

Named arguments

bool IsFlinstone(
    string firstName, 
    string lastName) {
    ...
}  
...
var fred = IsFlinstone(
    lastName: "Rubble", 
    firstName: "Barney");

Optional arguments

bool IsFlinstone(
    string firstName, 
    string lastName = "Flinstone") {
    ...
}
if(IsFlinstone("Fred")) {
...
}
if(IsFlinstone("Barney", "Rubble")) {
...
}

Covariance

public interface IEnumerator<out T> : 
    IDisposable, 
    IEnumerator {
        T Current { get; }
        bool MoveNext();
        void Reset();
}

Covariance Example

IEnumerable<String> strings = new List<String>();
IEnumerable<Object> objects = strings;

Contravariance

public interface IComparer<in T> {
    int Compare(T x, T y);
}

Contravariance Example

void PutOutside(object o) { .... }
Action<object> action = PutOutside;
Action<Flinstone> dinoAction = action;

.NET Framework

.NET Framework

.NET Framework

Task Parallel Library

Parallel.ForEach(sourceCollection, 
    item => Process(item));

Task<string> reportData2 = 
    Task.Factory.StartNew(() => GetFileData())
        .ContinueWith((x) => Analyze(x.Result))
        .ContinueWith((y) => Summarize(y.Result));

var pythagoreanTriples = 
    from a in Enumerable.Range(1, 25).AsParallel()
    from b in Enumerable.Range(a, 25 - a)
    from c in Enumerable.Range(b, 25 - b)
    where a * a + b * b == c * c
    select Tuple.Create(a, b, c);

F#

F# Features