- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Delegates, Events, and Lambda Expressions in Visual C#
Delegates, Events, and Lambda Expressions Printing QR Code JIS X 0510 In Visual C# Using Barcode printer for .NET framework Control to generate, create QR Code 2d barcode image in VS .NET applications. Scan QR In Visual C#.NET Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET framework applications. // Reverse a string static void Reverse(ref string s) { string temp = ""; int i, j; ConsoleWriteLine("Reversing string"); for(j=0, i=sLength-1; i >= 0; i--, j++) temp += s[i]; s = temp; } static void Main() { // Construct delegates StrMod strOp; StrMod replaceSp = ReplaceSpaces; StrMod removeSp = RemoveSpaces; StrMod reverseStr = Reverse; string str = "This is a test"; // Set up multicast strOp = replaceSp; strOp += reverseStr; // Call multicast strOp(ref str); ConsoleWriteLine("Resulting string: " + str); ConsoleWriteLine(); // Remove replace and add remove strOp -= replaceSp; strOp += removeSp; str = "This is a test"; // reset string // Call multicast strOp(ref str); ConsoleWriteLine("Resulting string: " + str); ConsoleWriteLine(); } } Draw Bar Code In C# Using Barcode generation for .NET Control to generate, create bar code image in .NET applications. Bar Code Reader In C# Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications. PART I PART I PART I
QR Code Generation In VS .NET Using Barcode printer for ASP.NET Control to generate, create QR image in ASP.NET applications. Print QR Code 2d Barcode In Visual Studio .NET Using Barcode printer for .NET framework Control to generate, create QR Code image in .NET framework applications. Here is the output: QR Code JIS X 0510 Generation In Visual Basic .NET Using Barcode generator for .NET Control to generate, create QR Code ISO/IEC18004 image in .NET framework applications. Code-39 Encoder In Visual C# Using Barcode creator for .NET framework Control to generate, create ANSI/AIM Code 39 image in Visual Studio .NET applications. Replacing spaces with hyphens Reversing string Resulting string: tset-a-si-sihT Reversing string Removing spaces Resulting string: tsetasisihT GS1 128 Creator In C#.NET Using Barcode creator for Visual Studio .NET Control to generate, create UCC - 12 image in Visual Studio .NET applications. 1D Barcode Creation In C# Using Barcode maker for Visual Studio .NET Control to generate, create Linear 1D Barcode image in Visual Studio .NET applications. Part I: Generating Barcode In Visual C#.NET Using Barcode maker for .NET framework Control to generate, create bar code image in VS .NET applications. EAN-8 Supplement 2 Add-On Generation In Visual C#.NET Using Barcode generation for VS .NET Control to generate, create EAN-8 Supplement 2 Add-On image in .NET applications. The C# Language
EAN 128 Creator In None Using Barcode creation for Font Control to generate, create EAN 128 image in Font applications. Recognize Code 128C In .NET Framework Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications. In Main( ), four delegate instances are created One, strOp, is null The other three refer to specific string modification methods Next, a multicast is created that calls RemoveSpaces( ) and Reverse( ) This is accomplished via the following lines: Bar Code Creation In .NET Framework Using Barcode generation for ASP.NET Control to generate, create bar code image in ASP.NET applications. Recognize Barcode In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. strOp = replaceSp; strOp += reverseStr; USS-128 Drawer In Objective-C Using Barcode generator for iPhone Control to generate, create GTIN - 128 image in iPhone applications. Data Matrix 2d Barcode Drawer In None Using Barcode maker for Office Word Control to generate, create Data Matrix image in Word applications. First, strOp is assigned replaceSp Next, using +=, reverseStr is added When strOp is invoked, both methods are invoked, replacing spaces with hyphens and reversing the string, as the output illustrates Next, replaceSp is removed from the chain, using this line: Data Matrix Generation In Java Using Barcode creator for BIRT Control to generate, create DataMatrix image in Eclipse BIRT applications. Painting UPC A In Java Using Barcode drawer for Java Control to generate, create Universal Product Code version A image in Java applications. strOp -= replaceSp; and removeSP is added using this line: strOp += removeSp; Then, strOp is again invoked This time, spaces are removed and the string is reversed Delegate chains are a powerful mechanism because they allow you to define a set of methods that can be executed as a unit This can increase the structure of some types of code Also, as you will soon see, delegate chains have a special value to events Covariance and Contravariance
There are two features that add flexibility to delegates: covariance and contravariance Normally, the method that you pass to a delegate must have the same return type and signature as the delegate However, covariance and contravariance relax this rule slightly, as it pertains to derived types Covariance enables a method to be assigned to a delegate when the method s return type is a class derived from the class specified by the return type of the delegate Contravariance enables a method to be assigned to a delegate when a method s parameter type is a base class of the class specified by the delegate s declaration Here is an example that illustrates both covariance and contravariance: // Demonstrate covariance and contravariance using System; class X { public int Val; } // Y is derived from X class Y : X { } // This delegate returns X and takes a Y argument delegate X ChangeIt(Y obj); class CoContraVariance { // This method returns X and has an X parameter 15: Delegates, Events, and Lambda Expressions
static X IncrA(X obj) { X temp = new X(); tempVal = objVal + 1; return temp; } // This method returns Y and has a Y parameter static Y IncrB(Y obj) { Y temp = new Y(); tempVal = objVal + 1; return temp; } static void Main() { Y Yob = new Y(); // In this case, the parameter to IncrA // is X and the parameter to ChangeIt is Y // Because of contravariance, the following // line is OK ChangeIt change = IncrA; X Xob = change(Yob); ConsoleWriteLine("Xob: " + XobVal); // In the next case, the return type of // IncrB is Y and the return type of // ChangeIt is X Because of covariance, // the following line is OK change = IncrB; Yob = (Y) change(Yob); ConsoleWriteLine("Yob: " + YobVal); } }
|
|