C# 0 The Complete Reference


Compile( ) method defined by  Expression



Download 4,07 Mb.
Pdf ko'rish
bet734/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   730   731   732   733   734   735   736   737   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Compile( )

method defined by 



Expression

. It returns a reference that can be assigned to a delegate and 

then executed. You can declare your own delegate type or use one of the predefined 

Func

delegate types defined within the 



System

 namespace. Two forms of the 



Func

 delegate were 

mentioned earlier, when the query methods were described. Here is a list of all its forms:

delegate TResult Func( )

delegate TResult Func( )

delegate TResult Func( )

delegate TResult Func( )

delegate TResult Func( )

These forms represent methods that return a value and take from zero to four parameters 

(whose types are 



T1

 through 



T4

). If your expression requires more than four parameters, 

then you will need to define your own delegate type.

Expression trees have one key restriction: Only expression lambdas can be represented 

by expression trees. They cannot be used to represent statement lambdas.

Here is a simple example of an expression tree in action. It creates an expression tree 

whose data represents a method that determines if one integer is a factor of another. It then 

compiles the expression tree into executable code. Finally, it demonstrates the compiled code.

// A simple expression tree.

using System;

using System.Linq;

using System.Linq.Expressions;

www.freepdf-books.com



582

 

P a r t   I :  



T h e   C #   L a n g u a g e

class SimpleExpTree {

  static void Main() {

    // Represent a lambda expression as data.

    Expression>

      IsFactorExp = (n, d) => (d != 0) ? (n % d) == 0 : false;

    // Compile the expression data into executable code.

    Func IsFactor = IsFactorExp.Compile();

    // Execute the expression.

    if(IsFactor(10, 5))

      Console.WriteLine("5 is a factor of 10.");

    if(!IsFactor(10, 7))

      Console.WriteLine("7 is not a factor of 10.");

    Console.WriteLine();

  }

}

The output is shown here:



5 is a factor of 10.

7 is not a factor of 10.

The program illustrates the two key steps in using an expression tree. First, it creates an 

expression tree by using this statement:

Expression>

  IsFactorExp = (n, d) => (d != 0) ? (n % d) == 0 : false;

This constructs a representation of a lambda expression in memory. As explained, this 

representation is data, not code. This representation is referred to by 



IsFactorExp

. The 


following statement converts the expression data into executable code:

Func IsFactor = IsFactorExp.Compile();

After this statement executes, the 

IsFactor

 delegate can be called to determine if one value is 

a factor of another.

One other point: Notice that 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   730   731   732   733   734   735   736   737   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish