12: Delegates and Lambda Expressions in C#.NET

Maker PDF 417 in C#.NET 12: Delegates and Lambda Expressions
12: Delegates and Lambda Expressions
Generating PDF-417 2d Barcode In Visual C#
Using Barcode maker for .NET Control to generate, create PDF 417 image in .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]); } } // }
Create Bar Code In Visual C#
Using Barcode generator for .NET framework Control to generate, create bar code image in VS .NET applications.
Note that C# 20 and above 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 above syntax (This will cause some of the remaining code not to compile on version 10 compilers without modification 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
Make PDF 417 In .NET
Using Barcode generation for ASP.NET Control to generate, create PDF-417 2d barcode image in ASP.NET applications.
Introducing Delegates
Draw PDF-417 2d Barcode In Visual Studio .NET
Using Barcode printer for Visual Studio .NET Control to generate, create PDF-417 2d barcode image in .NET applications.
Listing 1210: Using a Different ComparisonHandler-Compatible Method
Creating PDF417 In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create PDF 417 image in .NET applications.
using System; class DelegateSample { 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++) {
Creating Barcode In C#.NET
Using Barcode creator for VS .NET Control to generate, create barcode image in .NET framework applications.
12: Delegates and Lambda Expressions
Create Bar Code In Visual C#.NET
Using Barcode generator for VS .NET Control to generate, create bar code image in VS .NET applications.
ConsoleWrite("Enter an integer: "); items[i] = intParse(ConsoleReadLine()); } BubbleSort(items, AlphabeticalGreaterThan); for (i = 0; i < itemsLength; i++) { ConsoleWriteLine(items[i]); } } }
Code 3 Of 9 Printer In Visual C#.NET
Using Barcode creator for VS .NET Control to generate, create ANSI/AIM Code 39 image in VS .NET applications.
OUTPUT 121:
Code 128A Printer In C#
Using Barcode drawer for VS .NET Control to generate, create Code 128 Code Set B image in VS .NET applications.
Enter Enter Enter Enter Enter 1 12 13 4 5 an an an an an integer: integer: integer: integer: integer: 1 12 13 5 4
Draw Data Matrix 2d Barcode In Visual C#
Using Barcode generation for VS .NET Control to generate, create ECC200 image in .NET framework applications.
The alphabetic order is different from the numeric order Note how simple it was to add this additional sort mechanism, however, compared to the process used at the beginning of the chapter The only changes to create the alphabetical sort order were the addition of the AlphabeticalGreaterThan method and then passing that method into the call to BubbleSort()
Encode Barcode In Visual Basic .NET
Using Barcode printer for .NET Control to generate, create bar code image in .NET applications.
Anonymous Methods
ECC200 Maker In Java
Using Barcode drawer for Java Control to generate, create Data Matrix image in Java applications.
C# 20 and above include a feature known as anonymous methods These are delegate instances with no actual method declaration Instead, they are defined inline in the code, as shown in Listing 1211
Barcode Generation In Java
Using Barcode generation for Java Control to generate, create bar code image in Java applications.
Listing 1211: Passing an Anonymous Method
Generate Bar Code In Java
Using Barcode generation for Java Control to generate, create bar code image in Java applications.
class DelegateSample { //
Code 128C Creator In VS .NET
Using Barcode generation for .NET framework Control to generate, create Code 128C image in VS .NET applications.
Anonymous Methods
Paint European Article Number 13 In VS .NET
Using Barcode maker for ASP.NET Control to generate, create GS1 - 13 image in ASP.NET applications.
static void Main(string[] args) { int i; int[] items = new int[5]; ComparisonHandler comparisonMethod; for (i=0; i<itemsLength; i++) { ConsoleWrite("Enter an integer:"); items[i] = intParse(ConsoleReadLine()); } comparisonMethod = delegate(int first, int second) { return first < second; }; BubbleSort(items, comparisonMethod); for (i = 0; i < itemsLength; i++) { ConsoleWriteLine(items[i]); } } }
Bar Code Encoder In Visual Studio .NET
Using Barcode printer for ASP.NET Control to generate, create bar code image in ASP.NET applications.
In Listing 1211, you change the call to BubbleSort() to use an anonymous method that sorts items in descending order Notice that no LessThan() method is specified Instead, the delegate keyword is placed directly inline with the code In this context, the delegate keyword serves as a means of specifying a type of delegate literal, similar to how quotes specify a string literal You can even call the BubbleSort() method directly, without declaring the comparisonMethod variable (see Listing 1212)
Decode Code39 In VS .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.
Listing 1212: Using an Anonymous Method without Declaring a Variable
Generate EAN-13 In Java
Using Barcode creation for Java Control to generate, create EAN 13 image in Java applications.
class DelegateSample { // static void Main(string[] args) {
Print Code 128A In Visual Basic .NET
Using Barcode maker for .NET framework Control to generate, create USS Code 128 image in .NET framework applications.
12: Delegates and Lambda Expressions
int i; int[] items = new int[5]; for (i=0; i<itemsLength; i++) { ConsoleWrite("Enter an integer:"); items[i] = intParse(ConsoleReadLine()); } BubbleSort(items, delegate(int first, int second) { return first < second; } ); for (i = 0; i < itemsLength; i++) { ConsoleWriteLine(items[i]); } } }
Note that in all cases, the parameter types and the return type must be compatible with the ComparisonHandler data type, the delegate type of the second parameter of BubbleSort() In summary, C# 20 included a new feature, anonymous methods, that provided a means to declare a method with no name and convert it into a delegate
ADVANCED TOPIC Parameterless Anonymous Methods Compatibility of the method signature with the delegate data type does not exclude the possibility of no parameter list Unlike with lambda expressions, statement lambdas, and expression lambdas (see the next section), anonymous methods are allowed to omit the parameter list (delegate { return ConsoleReadLine() != ""}, for example) This is atypical, but it does allow the same anonymous method to appear in multiple scenarios even though the delegate type may vary Note, however, that although the parameter list may be omitted, the return type will still need to be compatible with that of the delegate (unless an exception is thrown)