Ratnakar’s Weblog

C# 3.0 demo

Posted in C# by Ratnakar Garikipati on August 24, 2007

My previous blog talks about the new C# language enhancements/extensions.

 

Here’s an excerpt from Luke’s C# demo at TechEd 2007 which covers things like Auto-Implemented properties, ‘var’ – Local Variable Type Inference, Collection Initializers, Object Initializers, Lambda Expressions etc

 

If you are a C# enthusiast…I would suggest you subscribe to his feeds at: http://blogs.msdn.com/lukeh/

 

using System;

//using System.Linq;

using System.Collections.Generic;

class Customer

{

    // C#3.0 Feature:  Auto-Implemented Properties

    public string CustomerID { get; set; }

    public string ContactName { get; set; }

    public string City { get; set; }

}

public delegate TResult Func<TArg, TResult>(TArg c);

static class Query

{

    // C#3.0 Feature:  Extension Methods

    public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T,bool> filter)

    {

        foreach (var item in source)

            if (filter(item))

                yield return item;

    }

    public static IEnumerable<TResult> Select<TSource,TResult>(this IEnumerable<TSource> source, Func<TSource,TResult> selector)

    {

        foreach (var item in source)

            yield return selector(item);

    }

}

class Program

{

    static void Main(string[] args)

    {

        List<Customer> customers = LoadCustomers();

        // C#3.0 Feature:  Query Expressions

        var query = from c in customers

                    where c.City == “London”

                    // C#3.0 Feature:  Anonymous Types

                    select new { c.CustomerID, c.ContactName };

        // C#3.0 Feature:  Lambda Expressions

        //var query = Query.Select(Query.Where(customers, c => c.City == “London”),

        //                         c => new { c.CustomerID, c.ContactName });

        //var query = customers.Where(c => c.City == “London”)

        //                     .Select(c => new { c.CustomerID, c.ContactName });

        foreach (var item in query)

            Console.WriteLine(“{0}, {1}”, item.CustomerID, item.ContactName);

    }

    private static List<Customer> LoadCustomers()

    {

        // C#3.0 Feature:  ‘var’ – Local Variable Type Inference

        // C#3.0 Feature:  Collection Initializers

        var customers = new List<Customer>()

        {

            // C#3.0 Feature:  Object Initializers

            new Customer { CustomerID = “ALFKI”, ContactName = “Maria Anders”, City = “Berlin”},

            new Customer { CustomerID = “ANATR”, ContactName = “Ana Trujillo”, City = “México D.F.”},

            new Customer { CustomerID = “WOLZA”, ContactName = “Zbyszek Piestrzeniewicz”, City = “Warszawa”}

        };

        return customers;

    }

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: