12: Delegates and Lambda Expressions in C#

Encode QR-Code in C# 12: Delegates and Lambda Expressions
12: Delegates and Lambda Expressions
Create QR Code In Visual C#.NET
Using Barcode generator for .NET Control to generate, create QR Code image in .NET applications.
Listing 127: Declaring a ComparisonHandler-Compatible Method
Create Bar Code In Visual C#.NET
Using Barcode printer for .NET framework Control to generate, create barcode image in VS .NET applications.
public delegate bool ComparisonHandler ( int first, int second);
QR Creation In .NET
Using Barcode creator for ASP.NET Control to generate, create QR image in ASP.NET applications.
class DelegateSample {
QR Code Printer In VS .NET
Using Barcode creation for Visual Studio .NET Control to generate, create QR-Code image in VS .NET applications.
public static void BubbleSort( int[] items, ComparisonHandler comparisonMethod) { // } public static bool GreaterThan(int first, int second) { return first > second; } // }
Make Denso QR Bar Code In VB.NET
Using Barcode printer for .NET framework Control to generate, create Quick Response Code image in .NET applications.
With this method defined, you can call BubbleSort() and pass the delegate instance that contains this method Beginning with C# 20, you simply specify the name of the delegate method (see Listing 128)
Create Barcode In Visual C#.NET
Using Barcode generator for Visual Studio .NET Control to generate, create barcode image in VS .NET applications.
Listing 128: Passing a Delegate Instance As a Parameter in C# 20
Bar Code Printer In Visual C#.NET
Using Barcode creator for Visual Studio .NET Control to generate, create barcode image in Visual Studio .NET applications.
public delegate bool ComparisonHandler ( int first, int second);
Generating ANSI/AIM Code 39 In Visual C#.NET
Using Barcode creator for .NET Control to generate, create USS Code 39 image in .NET framework applications.
class DelegateSample { public static void BubbleSort( int[] items, ComparisonHandler comparisonMethod) { // } public static bool GreaterThan(int first, int second) { return first > second; } static void Main() { int[] items = new int[100]; Random random = new Random();
ANSI/AIM Code 128 Generator In C#.NET
Using Barcode printer for VS .NET Control to generate, create ANSI/AIM Code 128 image in Visual Studio .NET applications.
Introducing Delegates
Generating Data Matrix ECC200 In Visual C#
Using Barcode creation for .NET Control to generate, create DataMatrix image in Visual Studio .NET applications.
for (int i = 0; i < itemsLength; i++) { items[i] = randomNext(intMinValue, intMaxValue); }
Barcode Generation In Java
Using Barcode creator for Java Control to generate, create bar code image in Java applications.
BubbleSort(items, GreaterThan); for (int i = 0; i < itemsLength; i++) { ConsoleWriteLine(items[i]); } } }
Bar Code Maker In VS .NET
Using Barcode encoder for .NET framework Control to generate, create bar code image in .NET applications.
Note that the ComparisonHandler delegate is a reference type, but you do not necessarily use new to instantiate it The facility to pass the name instead of using explicit instantiation is called delegate inference, a new syntax beginning with C# 20 With this syntax, the compiler uses the method name to look up the method signature and verify that it matches the method s parameter type
Print UPC-A Supplement 2 In Java
Using Barcode generator for Java Control to generate, create UPC A image in Java applications.
ADVANCED TOPIC Delegate Instantiation in C# 10 Earlier versions of the compiler require instantiation of the delegate demonstrated in Listing 129
Printing Code 128B In .NET
Using Barcode encoder for ASP.NET Control to generate, create Code 128 Code Set B image in ASP.NET applications.
Listing 129: Passing a Delegate Instance As a Parameter Prior to C# 20
Bar Code Maker In Java
Using Barcode creator for Java Control to generate, create bar code image in Java applications.
public delegate bool ComparisonHandler ( int first, int second);
Code 128 Code Set A Scanner In .NET Framework
Using Barcode scanner for .NET framework Control to read, scan read, scan image in .NET framework applications.
class DelegateSample { public static void BubbleSort( int[] items, ComparisonHandler comparisonMethod) { // } public static bool GreaterThan(int first, int second) {
Encode Code 128 Code Set C In Java
Using Barcode maker for Java Control to generate, create Code-128 image in Java applications.
12: Delegates and Lambda Expressions
Scanning Data Matrix ECC200 In .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in VS .NET applications.
return first > second; } static void Main(string[] args) { int i; int[] items = new int[5]; for (i=0; i<itemsLength; i++) { ConsoleWrite("Enter an integer:"); items[i] = intParse(ConsoleReadLine()); } BubbleSort(items, new ComparisonHandler(GreaterThan)); for (i = 0; i < itemsLength; i++) { ConsoleWriteLine(items[i]); } } // }
DataMatrix Generation In VB.NET
Using Barcode creator for Visual Studio .NET Control to generate, create DataMatrix image in .NET applications.
Note that C# 20 and later support both syntaxes, but unless you are writing backward-compatible code, the 20 syntax is preferable Therefore, throughout the remainder of the book, I will show only the C# 20 and later syntax (This will cause some of the remaining code not to compile on version 10 compilers, unless you modify those compilers to use explicit delegate instantiation) The approach of passing the delegate to specify the sort order is significantly more flexible than the approach listed at the beginning of this chapter With the delegate approach, you can change the sort order to be alphabetical simply by adding an alternative delegate to convert integers to strings as part of the comparison Listing 1210 shows a full listing that demonstrates alphabetical sorting, and Output 121 shows the results
Code 128C Encoder In Visual Basic .NET
Using Barcode creator for Visual Studio .NET Control to generate, create Code 128 Code Set C image in .NET applications.
Listing 1210: Using a Different ComparisonHandler-Compatible Method
using System; class DelegateSample {
Introducing Delegates
public delegate bool ComparisonHandler(int first, int second); public static void BubbleSort( int[] items, ComparisonHandler comparisonMethod) { int i; int j; int temp; for (i = itemsLength - 1; i >= 0; i--) { for (j = 1; j <= i; j++) { if (comparisonMethod(items[j - 1], items[j])) { temp = items[j - 1]; items[j - 1] = items[j]; items[j] = temp; } } } } public static bool GreaterThan(int first, int second) { return first > second; } public static bool AlphabeticalGreaterThan( int first, int second) { int comparison; comparison = (firstToString()CompareTo( secondToString())); return comparison > 0; } static void Main(string[] args) { int i; int[] items = new int[5]; for (i=0; i<itemsLength; i++) { ConsoleWrite("Enter an integer: "); items[i] = intParse(ConsoleReadLine()); }