We discussed that delegates are used to reference any C# Anonymous Methods that have the same signature as that of the delegate.
Anonymous methods provide a technique to pass a code block as a delegate parameter. Anonymous methods are the methods without a name, just the body.
You need not specify the return type in an anonymous method;
Writing C# Anonymous Methods
Anonymous methods are declared with the creation of the delegate instance, with a delegate keyword. For example,
delegate void NumberChanger(int n); ... NumberChanger nc = delegate(int x) { Console.WriteLine("Anonymous Method: {0}", x); };
The code block Console.WriteLine(“Anonymous Method: {0}”, x); is the body of the anonymous method.
For example,
nc(10);
Example
The following example demonstrates the concept −
using System; delegate void NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static void AddNum(int p) { num += p; Console.WriteLine("Named Method: {0}", num); } public static void MultNum(int q) { num *= q; Console.WriteLine("Named Method: {0}", num); } public static int getNum() { return num; } static void Main(string[] args) { //create delegate instances using anonymous method NumberChanger nc = delegate(int x) { Console.WriteLine("Anonymous Method: {0}", x); }; //calling the delegate using the anonymous method nc(10); //instantiating the delegate using the named methods nc = new NumberChanger(AddNum); //calling the delegate using the named methods nc(5); //instantiating the delegate using another named methods nc = new NumberChanger(MultNum); //calling the delegate using the named methods nc(2); Console.ReadKey(); } } }
The following result −
Anonymous Method: 10 Named Method: 15 Named Method: 30
Next Topic – Click Here
Pingback: C# - Generics - Adglob Infosystem Pvt Ltd
Pingback: Rust - Introduction - Adglob Infosystem Pvt Ltd
Pingback: Rust - HelloWorld Example - Adglob Infosystem Pvt Ltd
Pingback: Rust - Data Types - Adglob Infosystem Pvt Ltd
Pingback: Rust - Variables - Adglob Infosystem Pvt Ltd
Pingback: Rust - Constant - Adglob Infosystem Pvt Ltd
Pingback: Rust - String - Adglob Infosystem Pvt Ltd
Pingback: Rust - Operators - Adglob Infosystem Pvt Ltd
Pingback: Rust - Decision Making - Adglob Infosystem Pvt Ltd
Pingback: Rust - Loop - Adglob Infosystem Pvt Ltd
Pingback: Rust - Functions - Adglob Infosystem Pvt Ltd
Pingback: Rust - Tuple - Adglob Infosystem Pvt Ltd
Pingback: Rust - Array - Adglob Infosystem Pvt Ltd
Pingback: Rust - Ownership - Adglob Infosystem Pvt Ltd
Pingback: Rust - Borrowing - Adglob Infosystem Pvt Ltd
Pingback: Rust - Slices - Adglob Infosystem Pvt Ltd
Pingback: Rust - Structure - Adglob Infosystem Pvt Ltd
Pingback: Rust - Enums - Adglob Infosystem Pvt Ltd
Pingback: Rust - Modules - Adglob Infosystem Pvt Ltd
Pingback: Rust - Collections - Adglob Infosystem Pvt Ltd
Pingback: Rust - Error Handling - Adglob Infosystem Pvt Ltd
Pingback: Rust - Generic Types - Adglob Infosystem Pvt Ltd
Pingback: Rust - Input Output - Adglob Infosystem Pvt Ltd
Pingback: Rust - File Input/ Output - Adglob Infosystem Pvt Ltd
Pingback: Rust - Package Manager - Adglob Infosystem Pvt Ltd
Pingback: Rust - Iterator and Closure - Adglob Infosystem Pvt Ltd
Pingback: Rust - Smart Pointers - Adglob Infosystem Pvt Ltd