Top Banner
Jon Limjap Microsoft Most Valuable Professional for Visual C# Senior Application Developer, FBM e-Services Dynamics & Object Runtime Composition with C# 4.0
14

Dynamics & Object Runtime Composition with C# 4.0

May 25, 2015

Download

Technology

Jacinto Limjap

A discussion of the new language features of C# 4.0, emphasizing on the dynamic programming capabilities of the language and its ability to define objects at runtime
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dynamics & Object Runtime Composition with C# 4.0

Jon LimjapMicrosoft Most Valuable Professional for Visual C#

Senior Application Developer, FBM e-Services

Dynamics & Object Runtime Composition with C# 4.0

Page 2: Dynamics & Object Runtime Composition with C# 4.0

?use C#Do you

Page 3: Dynamics & Object Runtime Composition with C# 4.0

?dynamicstatic

Is C#...

Page 4: Dynamics & Object Runtime Composition with C# 4.0

static dynamicC# 4.0

Page 5: Dynamics & Object Runtime Composition with C# 4.0

Dynamic Language RuntimeMicrosoft

.NET

Page 6: Dynamics & Object Runtime Composition with C# 4.0

Why dynamic?

Page 7: Dynamics & Object Runtime Composition with C# 4.0

Tell me what to do……not how to do it

Expressiveness

Page 8: Dynamics & Object Runtime Composition with C# 4.0

If it quacks, it’s a duck!

Duck Typing

Page 9: Dynamics & Object Runtime Composition with C# 4.0

Object types can be modified on

runtime

Object Runtime Modification

Page 10: Dynamics & Object Runtime Composition with C# 4.0

dynamic myObj = "blah";

Dynamic at compile time

System.String at run time

Page 11: Dynamics & Object Runtime Composition with C# 4.0

dynamic myObj = "blah";Console.WriteLine(myObj.Contains("lah"));Console.WriteLine(myObj.IndexOf("x"));Console.WriteLine(myObj.GetType());

myObj = new List<int>();Console.WriteLine(myObj.Count);Console.WriteLine(myObj.GetType());

But no compiler errors!

Change of type here

Method signature doesn’t match initial type

Page 12: Dynamics & Object Runtime Composition with C# 4.0

dynamic mrFantastic = new ExpandoObject();mrFantastic.Name = "Reed Richards";mrFantastic.BirthDate =  new DateTime(year: 1961, month: 12, day: 2);

Console.WriteLine(mrFantastic.Name);Console.WriteLine(mrFantastic.BirthDate);

Requires System.Dynamic

Properties can be added during runtime

Page 13: Dynamics & Object Runtime Composition with C# 4.0

mrFantastic.SayMyName = new Action( () => Console.WriteLine(mrFantastic.Name));

mrFantastic.GetAge = new Func<int>(() => Convert.ToInt32( (DateTime.Now - mrFantastic.BirthDate).Days  / 365.25));

mrFantastic.SayMyName();Console.WriteLine(mrFantastic.GetAge());

Methods could be added using Action<T> & Func<T>

Page 14: Dynamics & Object Runtime Composition with C# 4.0

[email protected]://twitter.com/LaTtEX

http://dotnet.kapenilattex.com