123: BubbleSort() Method with Delegate Parameter in Visual C#.NET

Painting QR Code in Visual C#.NET 123: BubbleSort() Method with Delegate Parameter
Listing 123: BubbleSort() Method with Delegate Parameter
Generating QR Code ISO/IEC18004 In Visual C#.NET
Using Barcode encoder for VS .NET Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications.
class DelegateSample { // public static void BubbleSort( int[] items, ComparisonHandler comparisonMethod) { int i; int j; int temp; if(items==null) { return; } if(comparisonMethod == null) {
Make Bar Code In Visual C#
Using Barcode encoder for Visual Studio .NET Control to generate, create bar code image in .NET framework applications.
Introducing Delegates
Make QR Code JIS X 0510 In .NET
Using Barcode maker for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
throw new ArgumentNullException("comparisonMethod"); } 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; } } } } // }
Encoding QR Code 2d Barcode In .NET
Using Barcode creation for .NET Control to generate, create QR image in .NET framework applications.
ComparisonHandler is a data type that represents a method for comparing
QR Code Creator In Visual Basic .NET
Using Barcode printer for Visual Studio .NET Control to generate, create Denso QR Bar Code image in Visual Studio .NET applications.
two integers Within the BubbleSort() method you then use the instance of the ComparisonHandler, called comparisonMethod, inside the conditional expression Since comparisonMethod represents a method, the syntax to invoke the method is identical to calling the method directly In this case, comparisonMethod takes two integer parameters and returns a Boolean value that indicates whether the first integer is greater than the second one Perhaps more noteworthy than the particular algorithm, the ComparisonHandler delegate is strongly typed to return a bool and to accept only two integer parameters Just as with any other method, the call to a delegate is strongly typed, and if the data types do not match up, then the C# compiler reports an error Let us consider how the delegate works internally
Drawing Barcode In C#
Using Barcode encoder for .NET Control to generate, create barcode image in Visual Studio .NET applications.
Delegate Internals C# defines all delegates, including ComparisonHandler, as derived indirectly from SystemDelegate, as shown in Figure 1211 The first property is of type SystemReflectionMethodInfo, which I cover in 17 MethodInfo describes the signature of a particular method, including its name, parameters, and return type In addition to
Barcode Generator In Visual C#.NET
Using Barcode creation for VS .NET Control to generate, create bar code image in VS .NET applications.
1 The C# standard doesn t specify the delegate implementation s class hierarchy NET s implementation, however, does derive indirectly from SystemDelegate
Code 3/9 Printer In Visual C#
Using Barcode generation for Visual Studio .NET Control to generate, create Code 39 image in Visual Studio .NET applications.
12: Delegates and Lambda Expressions
Code 128B Generation In C#.NET
Using Barcode printer for .NET Control to generate, create Code 128 Code Set B image in .NET framework applications.
Figure 121: Delegate Types Object Model
DataMatrix Creator In C#
Using Barcode creator for VS .NET Control to generate, create Data Matrix 2d barcode image in .NET framework applications.
MethodInfo, a delegate also needs the instance of the object containing
Code 128 Code Set A Maker In .NET
Using Barcode creator for ASP.NET Control to generate, create Code-128 image in ASP.NET applications.
the method to invoke This is the purpose of the second property, Target In the case of a static method, Target corresponds to the type itself The purpose of the MulticastDelegate class is the topic of the next chapter It is interesting to note that all delegates are immutable Changing a delegate involves instantiating a new delegate with the modification included
Code 39 Extended Recognizer In .NET Framework
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
Defining a Delegate Type You saw how to define a method that uses a delegate, and you learned how to invoke a call to the delegate simply by treating the delegate variable as a method However, you have yet to learn how to declare a delegate data type For example, you have not learned how to define ComparisonHandler such that it requires two integer parameters and returns a bool Although all delegate data types derive indirectly from SystemDelegate, the C# compiler does not allow you to define a class that derives directly or indirectly (via SystemMulticastDelegate) from SystemDelegate Listing 124, therefore, is not valid
Draw Code39 In Visual Basic .NET
Using Barcode printer for VS .NET Control to generate, create Code39 image in Visual Studio .NET applications.
Introducing Delegates
Printing DataMatrix In VS .NET
Using Barcode generator for Visual Studio .NET Control to generate, create Data Matrix 2d barcode image in Visual Studio .NET applications.
Listing 124: SystemDelegate Cannot Explicitly Be a Base Class
Make DataMatrix In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create Data Matrix image in ASP.NET applications.
// ERROR: 'ComparisonHandler' cannot // inherit from special class 'SystemDelegate' public class ComparisonHandler: SystemDelegate { // }
Create Bar Code In Visual Basic .NET
Using Barcode generator for .NET Control to generate, create barcode image in Visual Studio .NET applications.
In its place, C# uses the delegate keyword This keyword causes the compiler to generate a class similar to the one shown in Listing 124 Listing 125 shows the syntax for declaring a delegate data type
Make ANSI/AIM Code 128 In VB.NET
Using Barcode maker for .NET framework Control to generate, create Code128 image in Visual Studio .NET applications.
Listing 125: Declaring a Delegate Data Type
UPCA Generator In Visual Studio .NET
Using Barcode creation for ASP.NET Control to generate, create UPCA image in ASP.NET applications.
public delegate bool ComparisonHandler ( int first, int second);
ECC200 Encoder In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create DataMatrix image in .NET applications.
In other words, the delegate keyword is shorthand for declaring a reference type derived ultimately from SystemDelegate In fact, if the delegate declaration appeared within another class, then the delegate type, ComparisonHandler, would be a nested type (see Listing 126)
Bar Code Drawer In VS .NET
Using Barcode creation for ASP.NET Control to generate, create barcode image in ASP.NET applications.
Listing 126: Declaring a Nested Delegate Data Type
class DelegateSample { public delegate bool ComparisonHandler ( int first, int second); }
In this case, the data type would be DelegateSampleComparisonHandler because it is defined as a nested type within DelegateSample
Instantiating a Delegate In this final step of implementing the BubbleSort() method with a delegate, you will learn how to call the method and pass a delegate instance specifically, an instance of type ComparisonHandler To instantiate a delegate, you need a method that corresponds to the signature of the delegate type itself In the case of ComparisonHandler, that method takes two integers and returns a bool The name of the method is not significant Listing 127 shows the code for a greater-than method