satisfy your technical curiosity c# 3.0 raj pai group program manager microsoft corporation

Post on 06-Jan-2018

216 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Satisfy Your Technical Curiosity C# 3.0 Design Goals Integrate objects, relational data, and XML Increase conciseness of language Add functional programming constructs Don’t tie language to specific APIs Remain 100% backwards compatible

TRANSCRIPT

Satisfy Your Technical Curiosity

C# 3.0C# 3.0Raj PaiRaj Pai

Group Program ManagerGroup Program ManagerMicrosoft CorporationMicrosoft Corporation

rajpai@microsoft.comrajpai@microsoft.com

Satisfy Your Technical Curiosity

The Evolution of C#The Evolution of C#

C# 1.0

C# 2.0

C# 3.0

Managed Code

Generics

Language Integrated Query

Satisfy Your Technical CuriositySatisfy Your Technical Curiosity

C# 3.0 Design GoalsC# 3.0 Design Goals

Integrate objects, relational data, and XMLIntegrate objects, relational data, and XML

Increase conciseness of languageIncrease conciseness of language

Add functional programming constructsAdd functional programming constructs

Don’t tie language to specific APIsDon’t tie language to specific APIs

Remain 100% backwards compatibleRemain 100% backwards compatible

Satisfy Your Technical Curiosity

Satisfy Your Technical Curiosity

Lambda ExpressionsLambda Expressionspublic delegate bool Predicate<T>(T obj);

public class List<T>{ public List<T> FindAll(Predicate<T> test) { List<T> result = new List<T>(); foreach (T item in this) if (test(item)) result.Add(item); return result; } …}

Satisfy Your Technical Curiosity

Lambda ExpressionsLambda Expressionspublic class MyClass{ public static void Main() { List<Customer> customers = GetCustomerList(); List<Customer> locals = customers.FindAll( new Predicate<Customer>(StateEqualsWA) ); }

static bool StateEqualsWA(Customer c) { return c.State == "WA"; }}

Satisfy Your Technical Curiosity

Lambda ExpressionsLambda Expressionspublic class MyClass{ public static void Main() { List<Customer> customers = GetCustomerList(); List<Customer> locals = customers.FindAll( delegate(Customer c) { return c.State == "WA"; } ); }}

Satisfy Your Technical Curiosity

Lambda ExpressionsLambda Expressionspublic class MyClass{ public static void Main() { List<Customer> customers = GetCustomerList(); List<Customer> locals = customers.FindAll(c => c.State == "WA"); }}

Lambda expression

Satisfy Your Technical Curiosity

Extension Methods

Satisfy Your Technical Curiosity

Extension MethodsExtension Methods

namespace MyStuff{ public static class Extensions { public static string Concatenate(this IEnumerable<string> strings, string separator) {…} }}

using MyStuff;

string[] names = new string[] { "Axel", "Mia", "Niels" };string s = names.Concatenate(", ");

Extensionmethod

Brings extensions into scope

obj.Foo(x, y)XXX.Foo(obj, x, y)

IntelliSense!

Satisfy Your Technical Curiosity

Object Initializers

Satisfy Your Technical Curiosity

Object InitializersObject Initializerspublic class Point{ private int x, y;

public int X { get { return x; } set { x = value; } } public int Y { get { return y; } set { y = value; } }}

Point a = new Point { X = 0, Y = 1 };

Point a = new Point();a.X = 0;a.Y = 1;

Field or property assignments

Satisfy Your Technical Curiosity

Collection InitializersCollection Initializers

List<int> numbers = new List<int> { 1, 10, 100 };

Must implement IEnumerable

List<int> numbers = new List<int>();numbers.Add(1);numbers.Add(10);numbers.Add(100);

Must have public Add method

Dictionary<int, string> spellings = new Dictionary<int, string> { { 0, "Zero" }, { 1, "One" }, { 2, "Two" }, { 3, "Three" } };

Add can take more than one parameter

Satisfy Your Technical Curiosity

Anonymous Types

Satisfy Your Technical Curiosity

IEnumerable<Contact> phoneListQuery = from c in customers where c.State == "WA" select new Contact { Name = c.Name, Phone = c.Phone };

Anonymous TypesAnonymous Types

public class Contact{ public string Name; public string Phone;}

+

Satisfy Your Technical Curiosity

var phoneListQuery = from c in customers where c.State == "WA" select new { Name = c.Name, Phone = c.Phone };

Anonymous TypesAnonymous Typesclass XXX{ public string Name; public string Phone;}

IEnumerable<XXX>

foreach (var entry in phoneListQuery) { Console.WriteLine(entry.Name); Console.WriteLine(entry.Phone);}

XXX

Satisfy Your Technical Curiosity

Local Variable Type Inference

Satisfy Your Technical Curiosity

Local Variable Type InferenceLocal Variable Type Inferenceint i = 5;string s = "Hello";double d = 1.0;int[] numbers = new int[] {1, 2, 3};Dictionary<int,Order> orders = new Dictionary<int,Order>();

var i = 5;var s = "Hello";var d = 1.0;var numbers = new int[] {1, 2, 3};var orders = new Dictionary<int,Order>();

“The type on the right hand side”

Satisfy Your Technical Curiosity

Query Expressions

Satisfy Your Technical Curiosity

Query ExpressionsQuery Expressions

from id in source{ from id in source | join id in source on expr equals expr [ into id ] | let id = expr | where condition | orderby ordering, ordering, … } select expr | group expr by key[ into id query ]

Starts with from Zero or more from,

join, let, where, or orderby

Ends with select or group by

Optional into continuation

Satisfy Your Technical CuriositySatisfy Your Technical Curiosity

from c in customerswhere c.State == "WA"select new { c.Name, c.Phone };

customers.Where(c => c.State == "WA").Select(c => new { c.Name, c.Phone });

Query ExpressionsQuery Expressions

Queries translate to method invocationsQueries translate to method invocationsWhere, Join, OrderBy, Select, GroupBy, …Where, Join, OrderBy, Select, GroupBy, …

Satisfy Your Technical Curiosity

Expression TreesExpression TreesCode as DataCode as Data

Predicate<Customer> test = c => c.State == "WA";

Predicate<Customer> test = new Predicate<Customer>(XXX);

private static bool XXX(Customer c) { return c.State == "WA";}

public delegate bool Predicate<T>(T item);

Satisfy Your Technical Curiosity

Expression TreesExpression TreesCode as DataCode as Data

Expression<Predicate<Customer>> test = c => c.State == "WA";

public delegate bool Predicate<T>(T item);

ParameterExpression c = Expression.Parameter(typeof(Customer), "c");Expression expr = Expression.Equal( Expression.Property(c, typeof(Customer).GetProperty("State")), Expression.Constant("WA") );Expression<Predicate<Customer>> test = Expression.Lambda<Predicate<Customer>>(expr, c);

Satisfy Your Technical Curiosity

Automatic Properties

Satisfy Your Technical Curiosity

Automatic propertiesAutomatic properties

public class Product{ public string Name; public decimal Price;}

Satisfy Your Technical Curiosity

Automatic propertiesAutomatic properties

public class Product{ string name; decimal price;

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

public decimal Price { get { return price; } set { price = value; } }}

Satisfy Your Technical Curiosity

Automatic propertiesAutomatic properties

public class Product{ public string Name { get; set; } public decimal Price { get; set; }}

private string □;

public string Name { get { return □; } set { □ = value; }}

Must have both get and set

Satisfy Your Technical Curiosity

Partial Methods

Satisfy Your Technical Curiosity

Partial MethodsPartial Methods

partial class Customer{ public string Name { get { … } set { _name = value; } }}

Satisfy Your Technical Curiosity

Partial MethodsPartial Methods

partial class Customer{ public string Name { get { … } set { OnNameChanging(value); _name = value; OnNameChanged(); } } partial void OnNameChanging(string value); partial void onNameChanged();}

Partial Method Call

Partial Method Definition

Satisfy Your Technical Curiosity

C# 3.0 Language InnovationsC# 3.0 Language Innovationsvar contacts = from c in customers where c.State == "WA" select new { c.Name, c.Phone };

var contacts = customers .Where(c => c.State == "WA") .Select(c => new { c.Name, c.Phone });

Extension methods

Lambda expressions

Query expressions

Object initializers

Anonymous types

Local variable type inference

Expression Trees

Automatic Properties

PartialMethods

Satisfy Your Technical CuriositySatisfy Your Technical Curiosity

C# 3.0 Design GoalsC# 3.0 Design Goals

Integrate objects, relational data, and XMLIntegrate objects, relational data, and XML

Increase conciseness of languageIncrease conciseness of language

Add functional programming constructsAdd functional programming constructs

Don’t tie language to specific APIsDon’t tie language to specific APIs

Remain 100% backwards compatibleRemain 100% backwards compatible

Satisfy Your Technical Curiosity

Satisfy Your Technical Curiosity

More InformationMore InformationWednesday Thursday

DEV203LINQ Overview14:30 – 15:45

DEV307C# 3.0

9:00 – 10:15

DEV205Visual Studio Orcas

17:45 – 18:45

DEV204ADO.NET vNext10:45 – 12:00

DEV318Visual Basic 9.013:00 – 14:15

http://csharp.net

Satisfy Your Technical Curiosity

© 2006 Microsoft Corporation. All rights reserved.This presentation is for informational purposes only.MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.

top related