Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » LINQ » Chapter 12 - Delegates and Lambda Expressions

Chapter 12 - Delegates and Lambda Expressions

C# achieves the same functionality using a delegate, which encapsulates methods as objects, enabling an indirect method call bound at runtime.

Technologies: LINQ
Author Name: Michaelis Mark
Total downloads : 0
Total page views :  31215
Rating :
 3/5
This article has been rated :  2 times
  Add to Technorati Add to Technorati    Digg This Digg This    Add to del.icio.us Add to del.icio.us
    Rate this article Read/Post comments Support Us   Printable Version 

Chapter Contents
1. Introducing Delegates
2. Delegate Data Types
3. Delegate Instantiation in C# 1.0
4. Anonymous Methods
5. Lambda Expressions
6. Expression Lambdas
7. Anonymous Method Internals
8. Expression Trees
9. SUMMARY

DevExpress Free UI Controls
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

This chapter has been excerpted from book "Essential C# 4.0" with permission from Addison-Wesley"

Introducing Delegates

Veteran C and C++ programmers have long used method pointers as a means to pass executable steps as parameters to another method. C# achieves the same functionality using a delegate, which encapsulates methods as objects, enabling an indirect method call bound at runtime. Consider an example of where this is useful.

Defining the Scenario

Although not necessarily efficient, perhaps one of the simplest sort routines is a bubble sort. Listing 12.1 shows the BubbleSort() method.

Listing 12.1: BubbleSort() Method

    static class SimpleSort1
    {
        public static void BubbleSort(int[] items)
        {
            int i;
            int j;
            int temp;
            if (items == null)
            {
                return;
            }
            for (i = items.Length - 1; i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    if (items[j - 1] > items[j])
                    {
                        temp = items[j - 1];
                        items[j - 1] = items[j];
                        items[j] = temp;
                    }
                }
            }
        }
        // ...
    }

This method will sort an array of integers in ascending order. However, if you wanted to support the option to sort the integers in descending order, you would have essentially two options. You could duplicate the code and replace the greater-than operator with a less-than operator. Alternatively, you could pass in an additional parameter indicating how to perform the sort, as shown in Listing 12.2.

Listing 12.2: BubbleSort() Method, Ascending or Descending

class SimpleSort2
{
public enum SortType
{
Ascending,
Descending
}
{
int i;
int j;
int temp;
if(items==null)
{
return;
}
for (i = items.Length - 1; i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
switch (sortOrder)
{
{
temp = items[j - 1];
items[j - 1] = items[j];
items[j] = temp;
}
break;
{
temp = items[j - 1];
public static void BubbleSort(int[] items, SortType sortOrder)
case SortType.Ascending :
if (items[j - 1] > items[j])
case SortType.Descending :
if (items[j - 1] < items[j])
items[j - 1] = items[j];
items[j] = temp;
}
break;
}
}
}
}
// ...
}

However, this handles only two of the possible sort orders. If you wanted to sort them alphabetically, randomize the collection, or order them via some other criterion, it would not take long before the number of Bubble- Sort() methods and corresponding SortType values would become cumbersome.

Total Pages : 9 12345


        Rate this article Read/Post comments Support Us   Printable Version
 Book Detail
Book Title : Essential C# 4.0
Author: Michaelis Mark
Publisher: Addison Wesley


 Post a new question or comment about this article
Subject:
Comment:
 [Top] Rate this article:-
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
Latest Comments:
Subject Posted By Posted On
Support Us
Tell your friends about us
Is this article is a Bad Submission report us here
Submit articles, tutorials, and tips
Help by answering questions on our discussion forums
Help us correct errors and broken links
Looking for a job? Post your resume here
Looking for developers? Post your job here

Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.