Difference between Func and Expression in C#
Ritesh Sharma
I am agreed with Karunanidhi K and addng some more difference:- Func<..> namespace is System.Expression <..> namespace is System.Linq.ExpressionsFunc gives you the possibility to use a delegate for execution. A short sample might be:Func operation = name => name.Equals("Something"); bool result = operation("Something");Although in most cases it is enough to use a simple delegate for later execution, sometimes the need to do more with it but invoke is showing up. Currently I was working with the Specification Pattern and came to a point where I had to combine different Lambdas via And, Or & Not. The Expression Class gave me everything I neede to acomplish my goal. So instead of the sample above you “simply” write:Expression> expression = name => name.Equals("Something"); Func compiledExpression = expression.Compile(); bool result = compiledExpression("Something");
Func creates an executable function. Expression> creates an expression tree that allows you to work with the code in the function as data. Expression Trees allow you to do things like LINQ to SQL and LINQ to XML by generating the underlying calls from your .NET code. •Expression> is an expression tree which represents the original source code (it is stored in a tree-like data structure that is very close to the original C# code). In this form, you can analyze the source code and tools like LINQ to SQL can translate the expression tree (source code) to other languages (e.g. SQL in case of LINQ to SQL, but you could also target e.g. JavaScript). •Func<...> is an ordinary delegate that you can execute. In this case, the compiler compiles the body of the function to intermediate language (IL) just like when compiling standard method
http://www.c-sharpcorner.com/UploadFile/tirthacs/expressionfunc-vs-func/
Func-Func in short is parameterized delegate. Example-IEnumerable numbers = new[] { 3, 4, 7, 1, 8, 10, 21, 5, 9, 11, 14, 19 }; Func, IEnumerable> getGreaterThanTen = nums => {var array = new List();nums.ToList().ForEach(x =>{if (x > 10)array.Add(x);});return array; };
Expressions:-An expression is any legal combination of symbols that represents a value. Each programming language and application has its own rules for what is legal and illegal. For example, in the C language x+5 is an expression, as is the character string "MONKEYS."Expressions are often classified by the type of value that they represent. For example: Boolean expressions : Evaluate to either TRUE or FALSE integer expressions: Evaluate to whole numbers, like 3 or 100 Floating-point expressions: Evaluate to real numbers, like 3.141 or -0.005 String expressions: Evaluate to character stringsFunctions: A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is so each function performs a specific task.A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.Example:-/* function returning the max between two numbers */ int max(int num1, int num2) {/* local variable declaration */int result;if (num1 > num2)result = num1;elseresult = num2;return result; }
Thank you Manju :)