Delegation makes the expansion of Bubble Sorting more elegant-use of the open and closed Principle

Source: Internet
Author: User

 

This article focuses on the application of delegation. Including simple definition, use, benefits of delegation, and relationship between delegation and Lambda. All knowledge points are simplified to heuristic comments, which are not difficult to understand and hope to help you.

/// <Summary>

/// Delegate makes the Sorting Code more elegant (easy to expand and easy to maintain)

/// Knowledge points included in this article:

/// 1. Delegate Definition

/// 2. Delegate benefits

/// 3. Lamda (including statements Lamda and expressions Lambda)

/// 4. Relationship between Lambda and Delegation

/// </Summary>

Class Program

{

// Define embedded Delegate (the Delegate is essentially a class; the compiler inherits from System. Delegate by default)

Public delegate bool ComparerionHandler (int first, int second );

 

Static void Main (string [] args)

{

 

Int [] beforeSortList = new int [3] {1, 3, 2 };

 

Console. WriteLine (string. Join (",", beforeSortList. ToArray ()));

 

/// Real-name delegate passing Parameters

// Int [] afterSortList = BubbleSort (beforeSortList, Asb );

// Int [] afterSortList = BubbleSort (beforeSortList, Desc );

 

/// Parameters passed by anonymous methods

// Int [] afterSortList = BubbleSort (beforeSortList, delegate (int first, int second) {return first> second ;});

// Int [] afterSortList = BubbleSort (beforeSortList, delegate (int first, int second) {return first <second ;});

 

/// Statement Lambda passing parameters (Lambda is a simplified syntax for anonymous methods)

Int [] afterSortList = BubbleSort (beforeSortList, (first, second) =>{ return first> second ;});

 

Console. WriteLine (string. Join (",", afterSortList. ToArray ()));

 

 

}

Bytes --------------------------------------------------------------------------------------------------------------------------

 

// Use delegation to achieve --> open to expansion

// Ascending

// Public static bool Asb (int first, int second)

//{

// Return first> second;

//}

 

// Descending order

Public static bool Desc (int first, int second)

{

Return first <second;

}

 

// Sort letters

 

// Other scalable sorting ......

 

 

Bytes --------------------------------------------------------------------------------------------------------------------------

// Bubble sort

// Use the delegate to achieve --> the modification is closed.

Public static int [] BubbleSort (int [] list, ComparerionHandler compareion)

{

Int temp;

 

For (int I = list. Length-1; I> = 0; I --)

{

For (int j = 1; j <= I; j ++)

{

If (compareion (list [j-1], list [j])

{

Temp = list [j];

List [j] = list [j-1];

List [j-1] = temp;

}

} Www.2cto.com

}

Return list;

}

 

}

From: Shui Mu Nian

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.