C# 4.0 - Cairo Code Camp 2010

Post on 27-Jun-2015

924 views 0 download

Transcript of C# 4.0 - Cairo Code Camp 2010

1

Productivity in C# 4.0

2

Mohammad Tayseer

3

Productivity

4

Productivity=

Value

5

C#

Object-Oriented Programming

Generics

Expression Trees

Nullable

Lambda Expressions

Anonymous Types

Extension Methods

LINQ

Delegates

Partial Classes

6

C# 4.0

7

1. Dynamic capabilities

2. Optional and named arguments

3. Co-variant and contra-variant

4. Better COM interoperability

8

Dynamic?

9

int x = …;x.GetType(); // Safex.SomeMethod(); // Compilation

// error

dynamic d = x;d.GetType(); // Safe

d.SomeMethod(); // Dispatched // at runtime

10

x.SomeMethod()

x has SomeMethod

x.TryInvokeMember(“SomeMethod”, …)

11

interface IDynamicMetaObjectProvider{

bool TryGetMember(…); bool TrySetMember(…); bool TryDeleteMember(…);  bool TryInvoke(…); bool TryInvokeMember(…); bool TryCreateInstance(…);  bool TryGetIndex(…); bool TrySetIndex(); bool TryDeleteIndex(…);… }

12

PythonBinder

COMBinder

ObjectBinder

Dynamic Language Runtime

IronPython

IronRuby C# VB.NET Others…

13

C# 4.0 Dynamic

?

14

C# 4.0

Static

Optional dynamic

15

When?

16

Productivity

17

int x = …;

dynamic d = x;

d.SomeMethod(); // Runtime // error

18

19

((MyCompany.MyClient.MyProject.Workflow.Activities.SqlSelect)

this.GetActivityByName(“SqlSelect1”)).RowsAffected > 1

20

((MyCompany.MyClient.MyProject.Workflow.Activities.SqlSelect)

this.GetActivityByName(“SqlSelect1”)).RowsAffected > 1

21

((MyCompany.MyClient.MyProject.Workflow.Activities.SqlSelect)

this.GetActivityByName(“SqlSelect1”)).RowsAffected > 1

22

This is too

complex

23

this.SqlSelect1.RowsAffected > 1

Runtime Lookup Recursive

24

((MyCompany.MyClient.MyProject.Workflow.Activities.SqlSelect)

this.GetActivityByName(“SqlSelect1”)).RowsAffected > 1

this.SqlSelect1.RowsAffected > 1

25

dynamic x = …;x.GetType();x.Property1 = x.Property2;x[“1”] = x[“value 2”];x(1, 2, 3)x++;

x.ExtensionMethod() // Runtime error

26

Dynamic Dispatch

Demo

27

Named & optional

parameters

28

chart.ChartWizard(range.CurrentRegion,MissingValue,MissingValue,MissingValue,MissingValue,MissingValue,MissingValue,"Memory Usage in " + Environment.MachineName,MissingValue,MissingValue,MissingValue);

29

30

chart.ChartWizard(range.CurrentRegion, // SourceMissingValue, // GalleryMissingValue, // FormatMissingValue, // PlotByMissingValue, // CategoryLabelsMissingValue, // SeriesLabelsMissingValue, // HasLegend// Title"Memory Usage in " + Environment.MachineName,MissingValue, // CategoryTitleMissingValue, // ValueTitleMissingValue); // ExtraTitle

31

Strongly-typednamed

parameters

32

chart.ChartWizard(Source: range.CurrentRegion,Gallery: MissingValue,Format: MissingValue,PlotBy: MissingValue,CategoryLabels: MissingValue,SeriesLabels: MissingValue,HasLegend: MissingValue,Title: "Memory Usage in " + Environment.MachineName,CategoryTitle: MissingValue,ValueTitle: MissingValue,ExtraTitle: MissingValue);

33

chart.ChartWizard(Source: range.CurrentRegion,Gallery: MissingValue,Format: MissingValue,PlotBy: MissingValue,CategoryLabels: MissingValue,SeriesLabels: MissingValue,HasLegend: MissingValue,Title: "Memory Usage in " + Environment.MachineName,CategoryTitle: MissingValue,ValueTitle: MissingValue,ExtraTitle: MissingValue);

34

35

Optional parameters

36

chart.ChartWizard(Source: range.CurrentRegion,Gallery: MissingValue,Format: MissingValue,PlotBy: MissingValue,CategoryLabels: MissingValue,SeriesLabels: MissingValue,HasLegend: MissingValue,Title: "Memory Usage in " + Environment.MachineName,CategoryTitle: MissingValue,ValueTitle: MissingValue,ExtraTitle: MissingValue);

37

chart.ChartWizard(Source: range.CurrentRegion,Title: "Memory Usage in " + Environment.MachineName);

38

public void ChartWizard(Source = null,Gallery = null,Format = null,PlotBy = null,CategoryLabels = null,SeriesLabels = null,HasLegend = null,Title = “”,CategoryTitle = null,ValueTitle = null,ExtraTitle = null)

39

Co-variance & Contra-variance

40

Back to Basics

41

Lion is-a Animal

Camel is-a Animal

42

public static string CleanAnimal(Animal a)

{…}

CleanAnimal(camel);CleanAnimal(lion);

43

Thus

Stack<Lion>is-a

Stack<Animal>

44

No

45

public static string CleanAll(Stack<Animal> animals)

{ … }

Stack<Lion> lions = { … };

CleanAll(lions);

46

public static string CleanAll(Stack<Animal> animals)

{animals.Push(new Camel());

…}

47

Stack<Lion> is-not

Stack<Animal>

48

IEnumerable<Lion> should-be

IEnumerable<Animal>

49

IEnumerable<Lion>,IEnumerator<Lion>

are read-only

50

Only in C# 4

51

interface IEnumerator<out T>// T only in output position{T Current { get; }}

52

interface IEnumerable<out T>{IEnumerator<T> GetEnumerator();}

53

Co-variance

54

Now Contra-variance

55

interface IPushable<in T>{void Push(T element);}

56

IPushable<Animal>is-a

IPushable<Lion>

57

IPushable<Lion> lions = animals;lions.Push(new Lion());

IPushable<Camel> camels = animals;

camels.Push(new Camel());

58

Limitations

59

1.Interfaces

2.Delegates

3.Reference types

60

Co-variance & Contra-variance

Demo

61

BetterCOM

Interoperability

62

1. Dynamic

2. Optional & named params

3. No PIA

4. Omitting ref

63

Demos

64

?

65

twitter.com/m_tayseer

http://spellcoder.com/blogs/tayseer

m_tayseer82@yahoo.com