How to Implement Extension Methods and Lambda Expressions concepts in Linq Query

Extension Methods:- The Extension Method, in .NET Framework allow you to add methods to existing types without creating a new derived type.Means You can easily extend the functionality of existing method.This is done by re-compiling and modifying the original type.
         The extension Method takes parameters in which first parameter defines the types that it extends.A class that defines the Linq extension method is called Enumerable class which is contained in the System.Linq Namespace.
Rules for Extension Methods:
  • Method must be static 
  • Method must be declared in static class.
  • Method must be taken the parameter
Difference between Extension Method and Normal Method:-
  1. In an Extension Method requires this keyword with the first parameter but in Normal method don't like that.
  2. The Extension Method can not access the private members of the type that it extends.
There are some steps to implement this concepts on your visual studio.
Step 1 :- First open your visual studio--> File --> New-->Projects-->Select console Application --> OK.
Step 2 :- Now write the c# codes in program.cs file as given below:-
using System;

public static class ExtExample
    public static string list_values(this string str)
        if (str.Length > 0)
            char[] List = str.ToCharArray();
            for (int i = 0; i < str.Length;i++)
            List[i] = char.ToUpper(List[i]);
            return new string(List);
        return str;
class Program
    static void Main()
        // Use the string extension method on this str variable
        Console.WriteLine("Enter a string in small or capital latter");
        string str = Console.ReadLine();
        str = str.list_values();        // This is called like an instance method

Step 3 :- Now Run the Program (press F5)--> Enter a string which contains the small characters-->you will see following output as shown below:-


Note:-In above program, i have converted small letter characters to capital letter characters.

Anonymous Methods:- Anonymous method allow you to handle an event rather having a separate event handler.
         Suppose you want to display some message to users at a click of a button ,ypu can handle it in a standard way with a delegate and event handler or you can also perform this action using anonymous method.
There are some points about anonymous method as given below:-
  • You can not have a jump statement,Break,Go to and continue in an anonymous method.
  • If your program contains same functionality more than once ,you should not use anonymous method.
  • You should also not use an unsafe code inside an anonymous method.
Benefits of Anonymous methods:-
  • It helps to reduce the codes .
  • No need to define any static event handlers in anonymous method.
Lambda Expression :-
Lambda Expression is the extension of the anonymous method.In .NET Framework 3.5 , a new syntax is introduce in anonymous method that is called Lambda expression. Lambda Expression is an anonymous function that can contain expression and statement to create delegates or expression tree types.It uses the Lambda operator (=>).
(Input parameter) => expression;
The Left had side of the Lambda operator specifies the Expression or the statement.
  • Here parentheses are optional if Lambda Expression has only one parameter.
  • If Lambda Expression contains more than one parameter then parentheses and commas  (') are required otherwise program will give error. 
Benefits of Lambda Expression:
  • The Lambda Expression allows you to declare your method code inline instead of with the delegate function.
  • It has more concise syntax to achieve same goal.
  • The Lambda Expression are very helpful in writing Linq query expression with a very compact and concise way.
Lambda Expression VS Anonymous method:-
  • The Lambda Expression is almost similar to anonymous method which was introduced with .NET 2.0.
  • The Lambda Expression provides a more concise and functional syntax for writing the anonymous method. 
  • There are no basic Difference between them.Means Lambda Expression is an anonymous function.
  • In Lambda Expression ,syntax elements are not required,which is automatically managed by the compiler.
Example of Lambda Expression:-
Step 1 :- Write the Lambda Expression codes in your console application(program.cs file) as show below:-
using System;
using System.Linq;
namespace Lambda_Expression
    class Program
        public delegate void del(string s);
        public delegate void del1();
        static void Main(string[] args)
        // First statement......
            del obj = (str) => Console.WriteLine(str);
            Console.WriteLine("Enter a statement");
            //call first delegate method which takes one parameter
       // second statement......
            del1 obj1 = () =>
                Console.WriteLine("Enter a Numeric integer Value");
                int j = int.Parse(Console.ReadLine());
                for (int i = 0; i < j; i++)
            //call first delegate method which takes zero argument
Step 2 :- Now Run the program(press F5) --> You will see following output as shown below:-

Note:-In above program , i have defined two delegate that contains one and zero argument respectively. I have implemented Lambda expression on both delegate method as shown in above output.

Download whole attached file from below


Post a Comment

Powered by Blogger.