5. .NET
Framework
C#
C++
VB.NET
IronRuby
...
Visual
Studio
.NET
Common
Type
System
&
Common
Intermediate
Language
Common
Language
Specifica2on
Base
Class
Libraries
Common
Language
Run2me
6. 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.
7. CIL
• Stack-‐based
language
• CIL
manipulaQon
enables
– Dynamic
code
generaQon,
e.g.
Fasterflect
– Code
instrumentaQon,
e.g.
test
coverage
tools
– Decompilers,
obfuscators…
8. InteresQng
Type
Concepts
• Value
types
vs.
reference
types
• Stack-‐allocated
vs.
heap-‐allocated
• Pass-‐by-‐value
vs.
pass-‐by-‐reference
• Delegate
vs.
event
types
15. Implicitly-‐typed
Local
Variables
• Benefits
– DRY
(Don’t
Repeat
Yourself)
• SpecificaQons
– Only
apply
to
local
variables
– Must
be
iniQalized
with
an
non-‐null
expression
17. Auto-‐implemented
ProperQes
• AIP
allows
us
to
declare
properQes
with
no
implementaQon
and
no
backing
fields:
compiler
will
generate
code
for
them
automaQcally
20. 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
21. Anonymous
Types
• This
feature
allows
us
to
quickly
create
objects
inside
a
method
without
declaring
their
corresponding
class
22. 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
26. 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);
}
27. LINQ
in
a
Nutshell
Integrate
query
expressions
into
languages
Unified
approach
to
query
data
Declara2ve
style
of
coding
Extensible
via
provider
mechanism
28. 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 });
29. LINQ
&
C#
3.0
var contacts =
from c in customers Query Expression
where c.State == "WA"
select new { c.Name, c.Phone };
Implicitly-typed Local
Variable
Lambda
Expression
var contacts =
customers
.Where(c => c.State == "WA")
.Select(c => new { c.Name, c.Phone });
Extension Object
Method Initializer
Anonymous
Type
30. 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>
31. LINQ
Clauses
from
let
where
join
select
orderby
group
32. Structure
of
LINQ
Queries
First
Line
• from
clause
Middle
Lines
• from,
where,
orderby,
join,
let
clauses
Last
Line
• select
or
group-by
clause
33. LINQ
Architecture
Visual Basic C# Others…
.NET Language-Integrated Query
LINQ to ADO.NET
LINQ LINQ
LINQ LINQ LINQ to XML
to Object
to SQL to DataSet to Entities
<book>
<Qtle/>
<author/>
<year/>
<price/>
</book>
Objects
RelaQonal
DB
XML
34. IEnumerable<T>
from
itemName
in
srcExpr
Object
where
predExpr
Object
orderby
(keyExpr
(ascending
|
descending))
Object
Objects
in
Memory
select
selExpr
...
where
where
orderby
where
orderby
select
35. IQueryable<T>
Nothing
In
from
itemName
in
srcExpr
where
predExpr
orderby
(keyExpr
(ascending
|
descending))
Memory
select
selExpr
...
“FROM
[srcExpr]
WHERE
...”
where
FROM
[srcExpr]
WHERE
predExpr
where
ORDER
BY
[keyExpr]”
orderby
SELECT
[selExpr]
where
FROM
[srcExpr]
WHERE
predExpr
ORDER
BY
[keyExpr]”
orderby
select
36. Deferred
vs.
Non-‐deferred
Operators
Deferred
Non-‐deferred
• Rule
of
thumb:
LINQ
query
syntax
• Rule
of
thumb:
scalar/single-‐value
or
IEnumerable<T>
return
return
or
conversion
• AsEnumerable, Cast, • Aggregate, All, Any,
Concat, DefaultIfEmpty, Average, Contains, Count,
Distinct, Empty, Except, ElementAt,
GroupBy, GroupJoin, ElementAtOrDefault, Last,
Intersect, Join, OfType, LastOrDefault, LongCount,
OrderBy, Max, Min, Single,
OrderByDescending, Range, SingleOrDefault, Sum,
Repeat, Reverse, Select, ToArray, ToDictionary,
SelectMany, Skip, ToList, ToLookup
SkipWhile, Take,
TakeWhile, ThenBy,
ThenByDescending, Union,
Where
36
43. OpQonal
Values
• Numeric
and
string
literals
• null
• Compile-‐Qme
constants
• Enum
members
• default(T)
• Parameterless
constructor
for
structs
44. 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
47. 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.
63. 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
68. 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
80. StaQc
vs.
Dynamic
Binding
Sta2c
Binding
Dynamic
Biding
• Compiler
figures
out
which
• All
bindings
happen
during
members
to
call
(binding
run
Qme
process)
– Defer
subtype
polymorphic
resoluQon
Qll
run
Qme
82. 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
85. 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
88. Process
in
a
nutshell
C#
dynamic
Dynamic
builds
Objects
User-‐defined
or
from
other
languages
compiled
uses
IDynamicMetaObjectProvider
Call
Sites
cached
Delegate
Expression
emits
Tree
DLR
C#
Binder
builds
91. 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
92. 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
96. Single
vs.
MulQple
Dispatch
Single
Dispatch
Mul2ple
Dispatch
• Method
is
selected
based
• Method
is
selected
based
on
the
runQme
type
of
the
on
both
the
runQme
type
of
target
object
the
target
object
and
those
of
the
method’s
arguments
98. 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
102. 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
105. 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#)