Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
    Single Page App
    Bootstrap
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> C# >> Lambda Expression in c#.
Lambda Expression in c#.

In this blog we will see that how to use lambda expression in c#. Here I will also display that how to use annoymouse expression and you will see the disfference between these two expression.
Views: 9009     Comments: 2
by Awadhendra Tiwari on 5/21/2011

Lambda expressions in c#

A lambda expression is an anonymous function that can contain expressions and statements and can be used to create delegates or expression tree type. All lambda expressions use the lambda operator =>, which is read as “goes to”. The left side of lambda operator specifies the input parameter if any and right side holds the expression or statement block.

The lambda expression has the same precedence as assignments (=) and is right associative. We can use lambdas in method based LINQ query as argument to slandered query. We cannot use lambda operators on the left side of is or as operator.

We can use following basic form of expression using lambda expression

1)      (input parameters) => expression: Here parenthesis are optional if there is only one parameter otherwise it is required.

2)      (x, y)=>x==y: We can use this type of lambda expressions when we want to specify input types to the compiler.

3)      ()=>SomeMethod(): When we want to create expression trees such as with sql server function then we use these type of lambda expression.

Example which represents lambda expression with collection

/// <summary>

        /// In this method we will see use of two method one is use of

        /// annoymouse method to search value in collection and other one

        /// is by using lambda expression to search. We see that using lambda

        /// expression line of code is saved.

        /// </summary>

        public void lambdaExpression()

        {

            //Create a collection objects.

            List<string> nameCollection = new List<string>();

            //Add some values in collection object.

            nameCollection.Add("James");

            nameCollection.Add("Chow");

            nameCollection.Add("John");

            nameCollection.Add("Haider");

            nameCollection.Add("Amit");

            //Using annoymouse expression for checking name

            string result = nameCollection.Find(delegate(string name)

            {

                return name.Equals("John");

            });

            if (!string.IsNullOrEmpty(result))

                Console.WriteLine(result);

            else

                Console.WriteLine("Specified record is not found.");

            //Using lambda expression for checking name

            string lambdaResult = nameCollection.Find(sname => sname.Equals("James1"));    //Here we directly write name of variable like sname.

            if (!string.IsNullOrEmpty(lambdaResult))

                Console.WriteLine(lambdaResult);

            else

                Console.WriteLine("Specified record is not found.");

        }

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by Awadhendra TiwariRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 4336
  
Copyright © 2014MindStick. All Rights Reserved.