For Asp.net Training with C# Click Here
Written By:-Isha Malhotra
Email:-malhotra.isha3388@gmail.com
How To Use Delegate and Its Type
Delegate is
a reference type which refer to a method. Delegates object hold the reference
of method of same signature. We invoke the method through delegate and this can
be determined at the runtime instead of compile time.
Note:-delegates can invoke both
static and non static method.
Syntax of delegate:-
Public
delegate return_typ delegate_name(parameters);
For Example:-
For non static method
Suppose we
are creating a class which contains some method for sum, div, sub and mul of
two numbers. We create delegate to hold the reference of these methods.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//delegate for holding method of same
signature
public delegate int calculation(int x, int y);
public class delegatePract
{
public int sum(int p, int q)
{
return p + q;
}
public int mul(int p, int q)
{
return p * q;
}
public int div(int p, int q)
{
return p / q;
}
public int sub(int p, int q)
{
return p - q;
}
}
Now create
web form and in page load create the object of class which holds the method and
create the object of delegate and pass the ref to this delegate and call the
method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
delegatePract dp=new
delegatePract();
calculation cal = new
calculation(dp.sum);
Response.Write(cal(5, 6));
}
}
In this
example first we create the object of class and after that we pass the
reference of sum method to the delegate object and invoke the method which
return the sum as we mentioned this definition in the method.
For static method
Now create
the class which contains the static method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public delegate int calculation(int x, int y);
public class delegatePract
{
public static int sum(int p, int q)
{
return p + q;
}
public static int mul(int p, int q)
{
return p * q;
}
public static int div(int p, int q)
{
return p / q;
}
public static int sub(int p, int q)
{
return p - q;
}
}
Now create
the object of delegate in page load and pass the reference of method with the
class name as we create static method and we can call static method with class
name.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
//creating the object of delegation
calculation cal = new
calculation(delegatePract.sum);
Response.Write(cal(5, 6));
}
}
Type of
Delegates:-
1. Single Cast Delegates:- single cast
delegates are those delegates which holds the reference of single method as
shown in the above example. It is derives from the System.Delegate name space.
2. Multi cast Delegates:-multicast
delegates contains the reference of multiple methods in series. We have to use
the + - and = operator to add method and remove method from this series.
For example:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
delegatePract dp=new
delegatePract();
calculation cal = new
calculation(dp.sum);
Response.Write(cal(5, 6));
cal
+= dp.mul;
cal
+= dp.sub;
cal
-= dp.mul;
Response.Write(cal(6, 6));
}
}
Webtrackker Technology is a leading mobile application development company offering integrated, dynamic and streamlined e-commerce solutions to businesses worldwide. Webtrackker takes pride in developing outstanding mobile applications using Android and iOS based platforms. We have created variety of core Android, core iOS and hybrid applications.
ReplyDeleteMobile Apps Development Company in Delhi NCR
PHP Training in Noida - We provides better PHP course covering the entire course content from basics to the advanced level. It’s better to choose the classroom PHP training that provides the PHP course in noida to get the practical knowledge of working experience.
ReplyDeleteJava Training in Noida - Croma campus by excellent experienced IT professionals who has more then 10+ Years of real time experience Our trainers has good training actuality so that best quality output will be delivered.s
ReplyDeleteI get a lot of great information from this blog. Thank you for your sharing this informative blog. Just now I have completed Hadoop certification course at iClass Gyansetu .
ReplyDeleteBigdata Hadoop training
Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
ReplyDeleteDocker Kubernetes training in Gurgaon
AWS cloud training in Gurgaon
FullStack Development Training in Gurgaon
One of the best courses for machine learning is Henry Harvin's Machine Learning using Python . They provide very good and quality content in each and every field .Thanks for sharing these helpful information .
ReplyDeleteThe best way to do it is actually doing it...
ReplyDeleteTake a course, ask a friend and learn the basics. Most copy programs and do things, instead start focusing on skills.
Dzone is a training institute and we have provided different types of projects to all students who can easily express their ideas and projects. We are the motive is we provide the best solution as per your needs and demands and then you can easily understand the whole concepts. This training just is affordable to all the students and then they can easily purchase and learn this concept.
ReplyDeletehttps://bit.ly/downloadITprojects
other link provide:-
http://bit.ly/digitaldzone
https://traininginstituteinjaipur.net/
Dzone is a training institute and we have provided different types of projects to all students who can easily express their ideas and projects. We are the motive is we provide the best solution as per your needs and demands and then you can easily understand the whole concepts. This training just is affordable to all the students and then they can easily purchase and learn this concept.
ReplyDeletehttps://bit.ly/downloadITprojects
other links provide:-
https://bit.ly/reactjsDzone
https://traininginstituteinjaipur.net/
https://www.dzone.co.in/courses/digital-marketing-training-institute-in-jaipur.aspx
TÜL PERDE MODELLERİ
ReplyDeleteSms onay
mobil ödeme bozdurma
NFT NASİL ALINIR
ANKARA EVDEN EVE NAKLİYAT
trafik sigortası
DEDEKTOR
websitesi kurmak
aşk kitapları
Thanks for sharing such captivating information with us. And I hope you will share some more information about. PHP Online Training please keep sharing!
ReplyDelete