ANONYMOUS METHODS OF C#
Anonymous Method
Delegates are a rattling multipurpose conception of the C# language. A delegate, with or without parameters, crapper be proclaimed in C# cipher in a kindred behavior to a method, eliminate that it contains no cipher block. The assign crapper be utilised to meaning a titled method, which module be executed when the assign is called. This leads to a enthusiastic care of flexibility, specially as you crapper modify the delegate’s referenced method at run-time.
An discernment of the ingest of delegates is also primary when creating and intense events. An circumstance proclaimed within a assemblage is ever supported upon a delegate. When another classes hold to the event, the methods to call on the upbringing of the circumstance are held in the inexplicit delegate.
Delegates crapper also utilised as parameters to whatever accepted methods. A beatific warning is the Sort method of the generic List assemblage in the .NET support edition 2.0. One of the full variations of this method accepts a Comparison delegate. The assign provided staleness meaning a method that performs a comparability of digit items and determines if they are coequal and, if they are not, which is the larger. This assign is executed as whatever nowadays as required to variety the assemblage according to the developer’s requirements.
Sometimes a method is created to be referenced by a assign but is exclusive utilised once. This crapper seem to be unnecessarily complicated and, for ultimate methods, crapper turn the understandability of the code. In these situations, it haw be desirable to ingest anonymous methods. An nameless method is a cipher country inserted into the information in locate of a delegate. It removes the responsibility to create a titled method elsewhere in the code.
Using Anonymous Methods
In the mass sections we module shew nameless method practice with whatever examples. The aforementioned examples crapper be institute in the maker cipher that crapper be downloaded using the unification at the crowning of the page.
Simple Anonymous Methods
The simplest modify of nameless method is digit that requires no parameters. These crapper be utilised to change delegates that also do not ingest parameters. A beatific warning of this is with digit artefact of play a newborn thread. Normally a method module be created that contains the cipher to be separate by the newborn thread. A Thread goal haw then be instantiated using a creator that accepts a assign as its parameter. The assign identifies the method that is executed when the arrange is started.
A ultimate warning haw exist of these digit elements as shown in the cipher below. Firstly, the method containing the cipher is created. In this example, the method only shows a communication before it completes:
private vacuum ThreadCode()
{
MessageBox.Show("New Thread!");
}
To create and move the newborn arrange to fulfil this method, the mass cipher haw be used:
Thread newThread = newborn Thread(ThreadCode); newThread.Start();
This creates a newborn thread, expiration the method as its assign parameter. Starting the arrange executes the “ThreadCode” method concurrently with the rest of the program.
The nameless method edition of the above cipher is simpler to read, as it does not order the creation of the “ThreadCode” method. Instead, the delegate keyword, followed by an blank ordered of parentheses () and a cipher country containing the cipher to execute, is utilised to change the assign in the constructor.
Thread newThread = newborn Thread(delegate() { MessageBox.Show("New Thread!"); });
newThread.Start();
For brief methods, this structure crapper simplify the cipher dramatically. For more Byzantine delegates or if the nameless method is to be utilised more than once, it crapper be more pertinent to tell a assign object. This goal crapper then be appointed an nameless method, kinda than a method reference. Using this deciding approach, the preceding warning becomes:
ThreadStart threadCode = delegate() { MessageBox.Show("New Thread!"); };
Thread newThread = newborn Thread(threadCode);
newThread.Start();