Sie sind auf Seite 1von 16

You want to use lambda expressions in the C# programming language, allowing functions to be used as data

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

;Func<int, int> func4 = (int x) => { return x + 1; }


;Func<int, int, int> func5 = (x, y) => x * y
;Console.WriteLine(func1.Invoke(1))
;Console.WriteLine(func2.Invoke(1))
;Console.WriteLine(func3.Invoke(1))
;Console.WriteLine(func4.Invoke(1))

Console.WriteLine(func5.Invoke(2, 2)

WPF

Win
Forms

DLR

ASP.NET

WCF

LINQ

And
!more

Base Class Libraries

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.

Named and Optional Parameters

3.

Improved COM Interop

4.

Covariance and Contravariance

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

:Most dynamic languages provide the following advantages for developers

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

Common Language Runtime

Ruby

Dynamically-Typed

Python

Ruby

Statically-Typed

C#

VB

Dynamic Language Runtime

Common Language Runtime

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

Dynamic Language Runtime


Expression
Expression Trees
Trees
Python
Binder

Ruby
Binder

Dynamic
Dynamic Dispatch
Dispatch
Object
Binder

Call
Call Site
Site Caching
Caching
JScript
Binder

COM
Binder

();Calculator calc = GetCalculator


;int sum = calc.Add(10, 20)
();object calc = GetCalculator
();Type calcType = calc.GetType
,"object res = calcType.InvokeMember("Add
,BindingFlags.InvokeMethod, null
);new object[] { 10, 20 }
();ScriptObject calc = GetCalculator
;int sum = Convert.ToInt32(res)
;object res = calc.Invoke("Add", 10, 20)
;int sum = Convert.ToInt32(res)

Statically
typed to be
dynamic

();dynamic calc = GetCalculator


;int sum = calc.Add(10, 20)

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