C# Beginner to advanced - Lesson 29 - Delegates

مجله خبری، آموزشی پرشین تک
منتشر شده در 11 خرداد 1397

Delegates:


A delegate is an object that can refer to a method.

The method can be called through this reference(delegate).

Same delegate can be used to call different methods during the runtime.

The method that will be invoked by a delegate is not determined at compile time, but rather at runtime.

The method invoking at runtime is the principal advantage of a delegate.

We can use delegates with static methods and instance methods.


delegate ret-type name(parameter-list);


Once created, a delegate instance can refer to and call methods whose return type and parameter list match those

specified by the delegate declaration.


Delegates in four steps:

  1. Defining the delegate

public delegate int Calculate (int value1, int value2);

  1. Creating methods which will be assigned to delegate object

//a method, that will be assigned to delegate objects

//having the EXACT signature of the delegate

public int add(int value1, int value2)

{

return value1 + value2;

}

//a method, that will be assigned to delegate objects

//having the EXACT signature of the delegate

public int sub( int value1, int value2)

{

return value1 - value2;

}


  1. Creating the delegate object and assigning methods to those delegate objects

//creating the class which contains the methods

//that will be assigned to delegate objects

MyClass mc = new MyClass();


//creating delegate objects and assigning appropriate methods

//having the EXACT signature of the delegate

Calculate add = new Calculate(mc.add);

Calculate sub = new Calculate(mc.sub);

  1. Calling the methods via delegate objects

//using the delegate objects to call the assigned methods

Console.WriteLine("Adding two values: " + add(10, 6)); or sum=add(10,6)

Console.WriteLine("Subtracting two values: " + sub(10,4)); or diff=sub(10,6)


Unicasting and Multicasting:


Unicasting means adding and invoking only one method.

Multicasting is the ability to create an invocation list, or chain, of methods that will be automatically called when a delegate is invoked.

Use the + or += operator to add methods to the chain.

To remove a method, use – or – =.

If the delegate returns a value, then the value returned by the last method in the list becomes the return value of the entire delegate invocation.


ankpro

ankpro training

Asp.net MVC

C#

C sharp

Bangalore

Rajajinagar

Selenium

Coded UI

Mobile automation testing

Mobile testing

JQuery

JavaScript

.Net

Components of the .Net framework

Hello World

Literal

Keywords

Variable

Data types

Operators

Branching

Loops

Arrays

ArrayList

Strings

String Builder

Structures

Enums

Functions

Classes

Inheritance

Polymorphism

Properties

Indexers

Events

Nested Classes

Delegates

Anonymous methods

Labda expressions

Abstract classes

Exception Handling

Linq

Interfaces

Extension methods

Anonymous types

Generics

Collections

Garbage Collection

Reflection

Attributes

Input and output statements

Type casting

Boxing and Unboxing

Var vs Dynamic vs Object

دیدگاه کاربران