Download - C# 3.0 and 4.0
![Page 1: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/1.jpg)
C# 3.0 & 4.0
Buu Nguyen
![Page 2: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/2.jpg)
Buu Nguyen
• Microso6 MVP ASP.NET/IIS 2010, 2011 • Vice President of Technology, KMS Technology • IT Lecturer, RMIT University Vietnam • www.buunguyen.net/blog • @buunguyen
![Page 3: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/3.jpg)
Agenda
• .NET and CLR • EvoluQon of C# • C# 3.0 and LINQ • C# 4.0 and Dynamic Programming
![Page 4: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/4.jpg)
.NET AND CLR
![Page 5: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/5.jpg)
.NET Framework
Common Language Run2me
Base Class Libraries
Common Type System & Common Intermediate Language
C# C++ VB.NET ... IronRuby
Visual Studio .NET
Common Language Specifica2on
![Page 6: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/6.jpg)
CTS and CLS
• The Common Type System (CTS) specifies type rules for .NET code. CIL code complies with CTS.
• The Common Language SpecificaQon (CLS), a subset of CTS, assures language interoperability.
![Page 7: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/7.jpg)
CIL
• Stack-‐based language • CIL manipulaQon enables
– Dynamic code generaQon, e.g. Fasterflect – Code instrumentaQon, e.g. test coverage tools – Decompilers, obfuscators…
![Page 8: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/8.jpg)
InteresQng Type Concepts
• Value types vs. reference types • Stack-‐allocated vs. heap-‐allocated • Pass-‐by-‐value vs. pass-‐by-‐reference • Delegate vs. event types
![Page 9: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/9.jpg)
More Types Post 1.x
• ParQal Types (C# 2.0) • Nullable Types (C# 2.0) • Generic Types (C# 2.0) • Dynamic Types (C# 4.0)
![Page 10: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/10.jpg)
EVOLUTION OF C#
![Page 11: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/11.jpg)
C# Language EvoluQon
C# 1.0
C# 2.0
C# 3.0
C# 4.0 C# 5.0
Generics (*) Nullable types Anonymous methods Yield return ParQal type StaQc class Namespace alias
LINQ (*) Auto-‐ properQes CollecQon iniQalizer Object iniQalizer Anonymous types Extension methods ParQal methods Lambda expressions Expression trees
Dynamic binding (*) Named arguments OpQonal parameters Generic variance Field-‐like events Robust locking Beeer COM interop
Async CaaS
![Page 12: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/12.jpg)
DirecQon
• Concurrent programming – FuncQonal-‐style programming in C# 3.0 – PLINQ in .NET 4.0
• Dynamic programming – Dynamic binding in C# 4.0 and CaaS in C# 5.0
![Page 13: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/13.jpg)
C# 3.0
![Page 14: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/14.jpg)
Features • Implicitly-‐typed Local Variables • Implicitly-‐typed Arrays • Auto-‐implemented ProperQes • Object IniQalizers • CollecQon IniQalizers • ParQal Methods • Anonymous Types • Extension Methods • Lambda Expressions • Expression Trees
![Page 15: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/15.jpg)
Implicitly-‐typed Local Variables
• Benefits – DRY (Don’t Repeat Yourself)
• SpecificaQons – Only apply to local variables – Must be iniQalized with an non-‐null expression
![Page 16: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/16.jpg)
Implicitly-‐typed Arrays
• More DRY
![Page 17: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/17.jpg)
Auto-‐implemented ProperQes
• AIP allows us to declare properQes with no implementaQon and no backing fields: compiler will generate code for them automaQcally
![Page 18: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/18.jpg)
Object IniQalizers
• Combine construcQon and property assignments
![Page 19: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/19.jpg)
CollecQon IniQalizers
• Like object iniQalizer, but this Qme applies to collecQons and maps
![Page 20: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/20.jpg)
ParQal Methods
• Method contracts – Work inside parQal classes – Must be private & return void – Can be sta,c or non-‐sta,c – Cannot have out parameter (support ref nonetheless)
– Must not be virtual, abstract, override, sealed, or new
• Can omit the implementaQon – All calls will be omieed by the compiler
![Page 21: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/21.jpg)
Anonymous Types
• This feature allows us to quickly create objects inside a method without declaring their corresponding class
![Page 22: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/22.jpg)
Extension Methods
• This feature enable us to add methods to exisQng types. This is useful when – We don’t have access to source code of a type – The type is sealed and can’t be inherited
![Page 23: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/23.jpg)
Lambda Expressions
• Shorter form of anonymous methods
![Page 24: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/24.jpg)
Expression Trees
• Code as data – Enable LINQ-‐2-‐SQL, DLR… – Can be compiled to delegate – “Statement trees” are supported in .NET 4.0
![Page 25: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/25.jpg)
LINQ
![Page 26: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/26.jpg)
Show me some LINQ!
var people = from p in GetSalesPeople() where p.HireDate > hireDate select p; foreach (SalesPerson person in people) { Console.WriteLine(person.FirstName); }
![Page 27: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/27.jpg)
LINQ in a Nutshell
Integrate query expressions into languages
Unified approach to query data
Declara2ve style of coding
Extensible via provider mechanism
![Page 28: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/28.jpg)
How is LINQ Implemented?
• LINQ Query Expressions are translated to normal invocaQons to Standard Query Operators
var 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 });
![Page 29: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/29.jpg)
LINQ & C# 3.0
var 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 Method
Lambda Expression
Query Expression
Object Initializer Anonymous
Type
Implicitly-typed Local Variable
![Page 30: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/30.jpg)
Where Do SQOs Come From?
§ Extensions methods for IEnumerable<T> defined in System.Linq.Enumerable
§ Building a LINQ-‐enabled data source is as simple as having your class implemenQng IEnumerable<T>
![Page 31: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/31.jpg)
LINQ Clauses
from
where
select
group orderby
join
let
![Page 32: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/32.jpg)
Structure of LINQ Queries
• from clause
First Line
• from, where, orderby, join, let clauses
Middle Lines
• select or group-by clause
Last Line
![Page 33: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/33.jpg)
LINQ Architecture
Objects
<book> <Qtle/> <author/> <year/> <price/> </book>
XML RelaQonal DB
LINQ to Object
LINQ to XML
LINQ to ADO.NET
LINQ to SQL
LINQ to DataSet
LINQ to Entities
Others… Visual Basic C#
.NET Language-Integrated Query
![Page 34: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/34.jpg)
IEnumerable<T>
Object Object Object Objects in Memory
from itemName in srcExpr where predExpr orderby (keyExpr (ascending | descending)) select selExpr ...
where
where orderby
where orderby select
![Page 35: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/35.jpg)
Nothing
In Memory
from itemName in srcExpr where predExpr orderby (keyExpr (ascending | descending)) select selExpr ...
where
where orderby
where orderby select
“FROM [srcExpr] WHERE ...”
SELECT [selExpr] FROM [srcExpr] WHERE predExpr ORDER BY [keyExpr]”
FROM [srcExpr] WHERE predExpr ORDER BY [keyExpr]”
IQueryable<T>
![Page 36: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/36.jpg)
Deferred vs. Non-‐deferred Operators
Deferred • Rule of thumb: LINQ query syntax
or IEnumerable<T> return • AsEnumerable, Cast,
Concat, DefaultIfEmpty, Distinct, Empty, Except, GroupBy, GroupJoin, Intersect, Join, OfType, OrderBy, OrderByDescending, Range, Repeat, Reverse, Select, SelectMany, Skip, SkipWhile, Take, TakeWhile, ThenBy, ThenByDescending, Union, Where
Non-‐deferred • Rule of thumb: scalar/single-‐value
return or conversion • Aggregate, All, Any,
Average, Contains, Count, ElementAt, ElementAtOrDefault, Last, LastOrDefault, LongCount, Max, Min, Single, SingleOrDefault, Sum, ToArray, ToDictionary, ToList, ToLookup
36
![Page 37: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/37.jpg)
C# 4.0
![Page 38: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/38.jpg)
Features Covered
• OpQonal Parameters • Named Arguments • Beeer COM Interop • Generic Variance • Robust Locking • Field-‐like Events
![Page 39: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/39.jpg)
OPTIONAL PARAMETERS
![Page 40: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/40.jpg)
How to implement MailMessage()?
![Page 41: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/41.jpg)
Overloading
![Page 42: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/42.jpg)
OpQonal Parameters
![Page 43: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/43.jpg)
OpQonal Values
• Numeric and string literals • null • Compile-‐Qme constants • Enum members • default(T) • Parameterless constructor for structs
![Page 44: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/44.jpg)
RestricQons
• OpQonal parameters must come a6er required parameters – Except for parameter array (empty array is used when not specified)
• OpQonal parameters can’t be ref or out
![Page 45: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/45.jpg)
In Depth: DefiniQon Site
![Page 46: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/46.jpg)
In Depth: Call Site
![Page 47: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/47.jpg)
Consequence
• OpQonal values are part of the published API, if they are changed, client code must be recompiled. Worse, the compiler won’t warn you if you don’t change.
![Page 48: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/48.jpg)
NAMED ARGUMENTS
![Page 49: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/49.jpg)
How to Avoid the Comments?
![Page 50: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/50.jpg)
Object IniQalizer
![Page 51: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/51.jpg)
Named Arguments
![Page 52: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/52.jpg)
…Named Arguments
![Page 53: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/53.jpg)
RestricQons
• PosiQonal arguments must come before named arguments
![Page 54: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/54.jpg)
Under the Hood
• The compiler just reorders, that’s all
![Page 55: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/55.jpg)
Consequence
• Parameter names are part of the published API, if they are changed, client code must be changed.
![Page 56: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/56.jpg)
BETTER COM INTEROPERABILITY
![Page 57: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/57.jpg)
How Do You Like This?
![Page 58: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/58.jpg)
#1: Omit ref
![Page 59: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/59.jpg)
#2: OpQonal Parameters
![Page 60: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/60.jpg)
GENERIC VARIANCE
![Page 61: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/61.jpg)
Is This Okay?
![Page 62: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/62.jpg)
No, because…
![Page 63: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/63.jpg)
Generic Covariance
• Generic covariance reserves assignment compaQbility on generic types – If T1 is a subtype of T2, GT<T1> behaves like subtype of GT<T2> on assignment
• Safe only when GT doesn’t “take in” any T
![Page 64: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/64.jpg)
Example
![Page 65: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/65.jpg)
On Delegate
![Page 66: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/66.jpg)
Is This Okay?
![Page 67: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/67.jpg)
No, because…
![Page 68: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/68.jpg)
Generic Contravariance
• Generic covariance reverses assignment compaQbility on generic types – If T1 is a subtype of T2, GT<T1> behaves like supertype of GT<T2> on assignment
• Safe only when GT doesn’t return any T
![Page 69: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/69.jpg)
Example
![Page 70: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/70.jpg)
On Delegate
![Page 71: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/71.jpg)
Under the Hood
![Page 72: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/72.jpg)
ROBUST LOCKING
![Page 73: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/73.jpg)
Problem
If the thread is aborted a>er the lock is acquired but before we enter the try block, we won’t have released the lock.
![Page 74: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/74.jpg)
SoluQon
![Page 75: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/75.jpg)
FIELD-‐LIKE EVENTS
![Page 76: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/76.jpg)
The Problem
Lock on ‘this’ is bad. Lock on ‘type’ is bad.
![Page 77: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/77.jpg)
The SoluQon
A simple lock would work, but this is beeer because it’s lock-‐free.
![Page 78: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/78.jpg)
DYNAMIC BINDING
![Page 79: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/79.jpg)
WHAT IS DYNAMIC BINDING
![Page 80: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/80.jpg)
StaQc vs. Dynamic Binding
Sta2c Binding • Compiler figures out which
members to call (binding process) – Defer subtype polymorphic
resoluQon Qll run Qme
Dynamic Biding • All bindings happen during
run Qme
![Page 81: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/81.jpg)
StaQc Binding
![Page 82: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/82.jpg)
Benefits of StaQc Binding
• Type and name errors are detected at compile Qme, e.g. – Invoke non-‐existent members – Pass in arguments with wrong type – Perform illegal cast
![Page 83: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/83.jpg)
Dynamic Binding
![Page 84: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/84.jpg)
HOW IS IT IMPLEMENTED?
![Page 85: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/85.jpg)
Run Time Binding
• Instead of aeempQng binding and generaQng CIL, the compiler packages the call and sends it to the Dynamic Language RunQme
• At run Qme, the DLR performs binding and execuQon
![Page 86: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/86.jpg)
Under the Hood
becomes
![Page 87: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/87.jpg)
The Dynamic Language RunQme
![Page 88: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/88.jpg)
Process in a nutshell
C# dynamic
Call Sites
C# Binder
Expression Tree
Delegate
Dynamic Objects
compiled
builds
emits
DLR
uses
builds
cached
User-‐defined or from other languages
IDynamicMetaObjectProvider
![Page 89: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/89.jpg)
Dynamic Type in CIL
![Page 90: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/90.jpg)
WHEN DO WE NEED IT?
![Page 91: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/91.jpg)
Key Scenarios
1. Access a member with only knowledge of its name, arguments, and target object
2. Interop with dynamic languages, e.g. IronRuby, IronPython
3. Have the target object decide how to respond to a call at run Qme
![Page 92: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/92.jpg)
Key Scenarios
1. Access a member with only knowledge of its name, arguments, and target object
2. Interop with dynamic languages, e.g. IronRuby, IronPython
3. Have the target object decide how to respond to a call at run Qme
![Page 93: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/93.jpg)
Access Members
![Page 94: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/94.jpg)
ReflecQon
![Page 95: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/95.jpg)
Dynamic Type
![Page 96: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/96.jpg)
Single vs. MulQple Dispatch
Single Dispatch • Method is selected based
on the runQme type of the target object
Mul2ple Dispatch • Method is selected based
on both the runQme type of the target object and those of the method’s arguments
![Page 97: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/97.jpg)
Dispatch Example
![Page 98: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/98.jpg)
Key Scenarios
1. Access a member with only knowledge of its name, arguments, and target object
2. Interop with dynamic languages, e.g. IronRuby, IronPython
3. Have the target object decide how to respond to a call at run Qme
![Page 99: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/99.jpg)
Invoke Ruby Code
![Page 100: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/100.jpg)
Work with Ruby Class
![Page 101: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/101.jpg)
Work with method_missing
![Page 102: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/102.jpg)
Key Scenarios
1. Access a member with only knowledge of its name, arguments, and target object
2. Interop with dynamic languages, e.g. IronRuby, IronPython
3. Have the target object decide how to respond to a call at run Qme
![Page 103: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/103.jpg)
The Magic Interface
IDynamicMetaObjectProvider
DynamicObject
ExpandoObject
![Page 104: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/104.jpg)
ExpandoObject
![Page 105: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/105.jpg)
DynamicObject’s OperaQons Name Descrip2on
TryGetMember Member geeer, e.g. obj.Name
TrySetMember Member seeer, e.g. obj.age = 10
TryDeleteMember Member removal (no equivalent in C#)
TryInvokeMember Method invocaQon, e.g. obj.Invoke()
TryConvert CasQng, e.g. (int)obj
TryCreateInstance Object creaQon (no equivalent in C#)
TryInvoke Self invocaQon, e.g. obj(10)
TryBinaryOperaQon Binary operaQon, e.g. obj + 10
TryUnaryOperaQon Unary operaQon, e.g. !obj
TryGetIndex Indexer geeer, e.g. obj[“key”]
TrySetIndex Indexer seeer, e.g. obj[“key”] = value
TryDeleteIndex Indexer removal (no equivalent in C#)
![Page 106: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/106.jpg)
Log Seeers & InvocaQons
![Page 107: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/107.jpg)
StaQcInvoker
![Page 108: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/108.jpg)
StaQcInvoker
![Page 109: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/109.jpg)
Close to the metal
![Page 110: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/110.jpg)
…Close to the metal
![Page 111: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/111.jpg)
…Close to the metal
![Page 112: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/112.jpg)
RESTRICTIONS
![Page 113: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/113.jpg)
RestricQon #1
• Doesn’t work with extension methods
![Page 114: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/114.jpg)
RestricQon #2
• Can’t resolve staQc members or constructors on a dynamic type
![Page 115: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/115.jpg)
RestricQon #3
• Method groups, anonymous methods and lambda expressions to be casted to exact type
![Page 116: C# 3.0 and 4.0](https://reader037.vdocuments.us/reader037/viewer/2022100304/554eba68b4c905977e8b553b/html5/thumbnails/116.jpg)
RestricQon #4