Understading Delegates


What are delegates?
Delegates are object that refer to an method. Normally we refer to objects, however referring to an object isn’t any different from referring a method they too have physical location in memory.

Why use delegate?
One delegate can be used to call different methods during runtime of a program by simply changing the method to which the delegate refers.
and Delegates Support Events.

delegate ret-type name(paramerter-list);
e.g. delegate string MyDelegate();

The MyDelegate can call any method whose return type is string and accepts no parameter. It can be instance method or a static method.

delegate string MyDelegate(String s);
class Program
{
static string GetNameLower(String s)
{
return s.ToLower() ;
}
static string GetNameUpper(string s)
{
return s.ToUpper();
}
static void Main(string[] args)
{
MyDelegate myD = new MyDelegate(GetNameLower); //or myD=GetNameLower
string s1 = myD(“Hi Nishant”);
Console.WriteLine(s1);
myD = new MyDelegate(GetNameUpper); //or myD=GetNameUpper
string s2 = myD(“Hi Nishant”);
Console.WriteLine(s2);
}
}

Understanding Multicasting

We can have chain of methods that will be called automatically when a delegate is invoked.
For this we will use += operator to add methods to chain and -= to remvove a method.
If delegate returns value than value returned by the last method becomes the return value of entire deleagation invocation. Thus a delegate making use of multicasting will have void as return type.

delegate void MyDelegate();
class Program
{
static void GetNameLower()
{
Console.WriteLine(“GetNameLower Called”);
}
static void GetNameUpper()
{
Console.WriteLine(“GetNameUpper Called”);
}
static void Main(string[] args)
{
MyDelegate myD = GetNameLower;
myD +=GetNameUpper;
myD(); //invoking the delegate
}

Advertisement

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

One thought on “Understading Delegates”

  1. Oi, achei seu blog pelo google está bem interessante gostei desse post. Gostaria de falar sobre o CresceNet. O CresceNet é um provedor de internet discada que remunera seus usuários pelo tempo conectado. Exatamente isso que você leu, estão pagando para você conectar. O provedor paga 20 centavos por hora de conexão discada com ligação local para mais de 2100 cidades do Brasil. O CresceNet tem um acelerador de conexão, que deixa sua conexão até 10 vezes mais rápida. Quem utiliza banda larga pode lucrar também, basta se cadastrar no CresceNet e quando for dormir conectar por discada, é possível pagar a ADSL só com o dinheiro da discada. Nos horários de minuto único o gasto com telefone é mínimo e a remuneração do CresceNet generosa. Se você quiser linkar o Cresce.Net(www.provedorcrescenet.com) no seu blog eu ficaria agradecido, até mais e sucesso. (If he will be possible add the CresceNet(www.provedorcrescenet.com) in your blogroll I thankful, bye friend).

    Like

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog & Website of Paul Andrew

Technical Leadership Centred Around the Microsoft Data Platform

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Dynamics 365 Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

%d bloggers like this: