Tuesday, 13 March 2012

What is Expression Tree why these Expression trees?


Expression tree is a data structure this data structure is constructed by the complier at complie at complie time.

When we write an expression in our code then we need to complie the expression using complie() method then which will create a data structure or additional code to our program. Using Invoke method we can invoke() that funcation.

When we complie the code automatically some code added our program  like Expression<type>  ……ect is added to our program. This added code is called using Invoke() method.

Func<int, int, int> expression = (a, b) => a + b;
       
 int i = function(45, 2);  // here no complie() and not required also.
 Console.Write(i);

public static int function(int a, int b)
        {
            return a + b;
        }
Here we are written the funcation() definication not by complier. Not requiered complie() method.
  Expression<Func<int, int,int>>  expression = (a, b) => a + b;        
  int m = expression.Compile().Invoke(41, 68);
  Console.Write(m);
Here Complier will generate the required method [like above funcation (int a,int b)] then invoked.
Expression<Func<int, int, int>> expressions = (a, b) => a + b;         
var s = expressions.Compile();   // here no complie()not required.
If we write our own method like aboveàfunction(int a,int b);
but no use prepare function is duty of expression on compling so this is bad pratice. so prepare function for expressions is duty of complier.
          int j = s.Invoke(45, 641);           
          Console.Write(j);

         var s = expressions.Compile();  
         int j = s.Invoke(45, 641);           
or
                        int  t=funcation(471,8);
Note: if our expression<genericdelegate> then our function is like above waste.
Some Examples:
            Func<int> kasi = () => 2 * 3 + 4;
            int res = kasi.Invoke();
            Console.WriteLine(res);------à 10
Or
Expression<Func<int>> babu = () => 2 * 3 + 4;
int res1 = babu.Compile().Invoke();
Console.WriteLine(res1);     -----------à 10
 Another example

            Expression<Func<int>> ravi = () => 4 + 5 + 6;
            var r = ravi.Compile();
            int k = r.Invoke();
            Console.Write(k);----------15
Or
Instead of Var use the that apporiate funcation delegate then invoke that delegate

ExDemo:  Expression<Func<int>> ravi = () => 4 + 5 + 6;
                Func<int> r1 = ravi.Compile();
            int k = r1.Invoke();
            Console.Write(k);-----------15
 Compile() method intelligence tells to us what is the return of our Expression            Func<int>. And Invoke method tell to return vlue also int K.
In this example we written code into 3 lines but one line of code is enough like
        Int K = expression.Compile().Invoke(41, 68);
           OR
      Int K = expression.Compile()(41,68); also

The Invoke() method tells the return type.this retun type changed according the type inside the Expression<Fun<string bool>> then Invoke return type is bool
Note whatever we pass last element is return type of invoke method.
In case of sinlge
Func<int>

return type also integer.
In case of Fun<int int> ----- return type also integer.
In case of Fun<int,bool> ----- return type also bool.
Ex:
String[] TestStrings = { "One", "Two", "Three", "Four", "Five", "Six" };
String Message = "";          
Expression<Func<String, Boolean>> ShortString = Value => Value.Length <= 3;
           foreach (String ThisString in TestStrings)
            {
                if (ShortString.Compile().Invoke(ThisString))
                    Message = Message + ThisString + "\r\n";
            }
            Console.Write(Message);
We know in linq prepares a query that can be executed at sql-server at that time these expressions so much help to for complex programs.

No comments:

Post a Comment