From Schmid.wiki
Jump to: navigation, search

Local Variable Type Inference

var list = new List<int>();

Is shorthand for:

List<int> list = new List<int>();

Collection Initializers

var list = new List<int> { 1, 2, 3 };

Is shorthand for:

var list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);

Automatic Properties

public string Name { get; private set; }

Is shorthand for:

public string Name {
    get { return name }
    private set { name = value; }
}
string name;

Lambda Expressions

list.FindAll(element => element > 10);

Is in normal contexts shorthand for:

listOfFoo.FindAll( delegate(int element) { return element > 10; } )

Example

This example demonstrates the following C# 3.0 features:

  • Local variable type inference
  • Collection initializer
  • Lambda expressions
using System;
using System.Collections.Generic;
using System.Text;

// To use foreach on a collection, we need to implement IEnumerable
class MyCollection : IEnumerable<int> {
    public void Add(int value) {
        list.Add(value);
    }
    List<int> list = new List<int>();

    public delegate bool MyPredicate(int obj);
    public MyCollection FindAll(MyPredicate findPredicate) {
        var result = new MyCollection();
        foreach (var element in list) {
            if (findPredicate(element))
                result.Add(element);
        }
        return result;
    }

    public delegate int Comparison(int a, int b);
    public void Sort(Comparison comparison) {
        list.Sort((a, b) => comparison(a, b));
    }

    public IEnumerator<int> GetEnumerator() {
        return new Enumerator(this);
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
        throw new NotImplementedException();
    }

    // IEnumerable must provide a GetEnumerator method that returns a class that
    // implements IEnumerator<int>.
    class Enumerator : IEnumerator<int> {

        // Inner class has private access to MyCollection, but needs a reference.
        public Enumerator(MyCollection collection) {
            this.collection = collection;
        }
        MyCollection collection;

        // Simple IEnumerator implementation.
        public int Current { get { return collection.list[current]; } }
        public bool MoveNext() {
            current += 1;
            return current < collection.list.Count;
        }
        public void Reset() { current = 0; }
        int current = 0;

        // Unused methods.
        public void Dispose() { }
        object System.Collections.IEnumerator.Current {
            get { throw new NotImplementedException(); }
        }
    }
}

class Program {
    static void Main(string[] args) {

        // Local variable type inference
        // Collection initializer
        var collection = new MyCollection { 7, 6, 4, 2, 20, 21, 32, 8, 11 };
        // Equivalent to:
        // List<int> list = new List<int>();
        // list.Add(7);
        // list.Add(6);
        // ...

        // Lambda expression
        var result = collection.FindAll(x => x > 5);
        // Equivalent to
        // var result = list.FindAll(delegate(int x) { return x > 10; });
        
        result.Sort((x, y) => x - y);
        Console.WriteLine("Find all:");

        foreach (var x in result) {
            Console.WriteLine(x);
        }
        // Equivalent to
        // var e = result.GetEnumerator();
        // while (e.MoveNext()) {
        //     Console.WriteLine(e.Current);
        // }
    }
}