The delegate is a reference type data type thatdefines the method signature. You can define variables of delegate, just like other data type, that can refer to any method with the same signature as the delegate.
There are three steps involved while working with delegates:
Declare a delegate
Create an instance and reference a method
Invoke a delegate
A delegate can be declared using the delegate keyword followed by a function signature, as shown below.
The following declares a delegate named MyDelegate.
Example: Declare a Delegate
publicdelegatevoid MyDelegate(stringmsg);
Above, we have declared a delegate MyDelegate with a void return type and a string parameter. A delegate can be declared outside of the class or inside the class. Practically, it should be declared out of the class.
After declaring a delegate, we need to set the target method or a lambda expression. We can do it by creating an object of the delegate using the new keyword and passing a method whose signature matches the delegate signature.
Example: Set Delegate Target
publicdelegatevoid MyDelegate(stringmsg); // declare a delegate// set target methodMyDelegate del = new MyDelegate(MethodA);
// orMyDelegate del = MethodA;
// or set lambda expressionMyDelegate del = (string msg) => Console.WriteLine(msg);
// target methodstaticvoid MethodA(string message)
{
Console.WriteLine(message);
}
You can set the target method by assigning a method directly without creating an object of delegate e.g., MyDelegate del = MethodA.
After setting a target method, a delegate can be invoked using the Invoke() method or using the () operator.
Example: Invoke a Delegate
del.Invoke("Hello World!");
// or
del("Hello World!");
C# contains reserved words that have special meaning for the compiler these reserved words are called "keywords" keywords can't be used as identifier (name of variable, class, interface etc) there are 11 types of keywords. 1. Modifier Keywords 2. Access Modifier Keywords 3. Statement Keywords 4. Method Parameter Keywords 5. Namespace Keywords 6. Operator Keywords 7. Access Keywords 8. Literal Keywords 9. Type Keywords 10. Contextual Keywords 11. Query Keywords 1. Modifier Keywords : are the specific keywords that indicates which can modify types and type members. Example - Abstract, async, const, event, extern, new, override, partial, read-only, sealed, static, unsafe, virtual and volatile. 2. Access Modifier Keywords: are applied to the declaration of the class, methods, properties, fields and other member. they define the accessibility of the class and its members. there are basic four type of Access Modifier...
It is a new feature that has been added in C# 3.0 which allows us to add new methods into a class without editing the source code of the class i.e. if a class consists of a set of members in it and in the future if you want to add new methods into the class, you can add those methods without making any changes to the source code of the class. Extension methods can be used as an approach to extending the functionality of a class in the future if the source code of the class is not available or we don’t have any permission in making changes to the class. Before extension methods, inheritance is an approach that is used for extending the functionality of a class i.e. if we want to add any new members to an existing class without making a modification to the class, we will define a child class to that existing class and then we add new members in the child class. In the case of an extension method, we will extend the functionality of an existing class. In this case, we will create a new cl...
Ref - Ref is two ways from caller to callee and back. static void Main(string[] args) { int outSideVar = 10; GetNextNameByOut(out outSideVar); Console.WriteLine(outSideVar); } static void GetNextNameByOut(out int inSideVar) { inSideVar = inSideVar + 20; } Out - Data sent from the caller has been discarded and its mandatory to initialize the variables inside the callee. Out is a ne way from callee to caller. static void Main(string[] args) ...
Comments
Post a Comment