c# - What's the purpose of the Expression class? -


i'm wondering difference between wrapping delegate inside expression<> , not ?

i'm seeing expression<foo> being used lot linq, far i've not found article explains difference between this, , using delegate.

e.g.

func<int, bool> is42 = (value) => value == 42; 

vs.

expression<func<int, bool>> is42 = (value) => value == 42; 

by storing lambda delegate, storing specific instance of delegate action. can't modified, call it. once have delegate, have limited options in inspecting , whatnot.

by storing lambda expression, storing expression tree represents delegate. can manipulated other things changing parameters, changing body , make radically different. compiled delegate may call if wish. can inspect expression see parameters are, , how it. query provider can use understand , translate expression language (such write sql query corresponding expression tree).

it whole lot easier to create delegate dynamically using expressions emitting code. can think of code @ higher level expressions similar how compiler views code instead of going low level , view code il instructions.

so expression, capable more simple anonymous delegate. though it's not free, performance take hit if run compiled expressions compared regular method or anonymous delegate. might not issue other benefits using expressions may important you.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -