Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
such as variables or fields. The lambda expression syntax in the C# language uses the => operator and this
specifies how the parameters and statement body of the anonymous function instance are separated, and
provides a formal name to the environment variables. Here we look at lambda expressions in the C# language
;using System
class Program
()static void Main
}
;Func<int, int> func1 = x => x + 1
;Func<int, int> func2 = x => { return x + 1; }
;Func<int, int> func3 = (int x) => x + 1
Console.WriteLine(func5.Invoke(2, 2)
WPF
Win
Forms
DLR
ASP.NET
WCF
LINQ
And
!more
The CLR
JIT &
NGEN
Garbage
Collector
Security
Model
Exception
Handling
Loader &
Binder
C# 4.0
Dynamic
C# 3.0
LINQ
C# 2.0
Generics
C# 1.0
Managed Code
1.
Late-Binding Support
2.
3.
4.
The dynamic language runtime (DLR) is a runtime environment that adds a set of services
for dynamic languages to the common language runtime (CLR). The DLR makes it easier to
develop dynamic languages to run on the .NET Framework and to add dynamic features to
.statically typed languages
Dynamic languages can identify the type of an object at run time, whereas in statically
typed languages such as C# and Visual Basic (when you use Option Explicit On) you must
, specify object types at design time. Examples of dynamic languages are Lisp, Smalltalk
.JavaScript, PHP, Ruby, Python, ColdFusion, Lua, Cobra, and Groovy
The ability to use a rapid feedback loop (REPL, or read-evaluate-print loop). This lets you
. enter several statements and immediately execute them to see the results
Support for both top-down development and more traditional bottom-up development. For
example, when you use a top-down approach, you can call functions that are not yet
. implemented and then add underlying implementations when you need them
Easier refactoring and code modifications, because you do not have to change static type
.declarations throughout the code
Dynamically-Typed
Python
Statically-Typed
C#
VB
Ruby
Dynamically-Typed
Python
Ruby
Statically-Typed
C#
VB
Stability Certain kinds of errors are now caught automatically by the compiler, before the code ever
. makes it anywhere close to production
Readability/Maintainability You are now providing more information about how the code is supposed to
work to future developers who read it. You add information that a specific variable is intended to hold
.a certain kind of value, and that helps programmers reason about what the purpose of that variable is
This is probably why, for example, Microsoft recommended VB6 programmers put a type prefix with the
.variable name, but that VB.Net programmers do not
Performance This is the weakest reason, but late-binding/duck typing can be slower. In the end, a
variable refers to memory that is structured in some specific way. Without strong types, the program
will have to do extra type verification or conversion behind the scenes at runtime as you use memory
. identical:that is logically structured one way as if it were structured in another
var is static typed - var s = "abc";
Console.WriteLine(s.Length);
string s = "abc";
;Console.WriteLine(s.Length)
dynamic s = "abc";
;Console.WriteLine(s.Length)
The DLR is pre-wired to know .NET objects, COM objects and so forth but any dynamic
language can implement their own objects or you can implement your own objects in
C# through the implementation of the IDynamicMetaObjectProvider interface. When an
object implements IDynamicMetaObjectProvider, it can participate in the resolution of
.how method calls and property access is done
The .NET Framework already provides two implementations of
:IDynamicMetaObjectProvider
DynamicObject : IDynamicMetaObjectProvider
The DynamicObject class enables you to define which operations can be performed on
dynamic objects and how to perform those operations. For example, you can define
what happens when you try to get or set an object property, call a method, or perform
.standard mathematical operations such as addition and multiplication
ExpandoObject : IDynamicMetaObjectProvider
The ExpandoObject class enables you to add and delete members of its instances at run
time and also to set and get values of these members. This class supports dynamic
binding, which enables you to use standard syntax like sampleObject.sampleMember,
.instead of more complex syntax like sampleObject.GetAttribute("sampleMember")
IronPython
IronRuby
#C
VB.NET
Others
Ruby
Binder
Dynamic
Dynamic Dispatch
Dispatch
Object
Binder
Call
Call Site
Site Caching
Caching
JScript
Binder
COM
Binder
Statically
typed to be
dynamic
Dynamic
conversion
Dynamic
method
invocation
Meta-programming
Language
Object Model
Source
File code
Source
Source code
Clas
Clas
s
s
public
public
Foo
Foo
Field
Field
privat
privat
e
e
X
X
Read-Eval-Print Loop
DSL Embedding
string
string
Compiler
.NET
Assembly
Source
code
Source code