delegate syntax and why should I need a delegate?

Hi there;

I couldn't remember the delegate syntax and I couldn't find out why I need a delegate.

I need an idiot proof definition and 6-year-old boy example so as to understand those.
Kind regards.
LVL 12
jazzIIIloveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

systanCommented:
LOOK how the Calculate moves;
sample1 from http://geekswithblogs.net/joycsharp/archive/2008/02/15/simple-c-delegate-sample.aspx
1. Defining the delegate

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

2. 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;            
}

 
3. 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);

 

4. Calling the methods via delegate objects

//using the delegate objects to call the assigned methods 
Console.WriteLine("Adding two values: " + add(10, 6));
Console.WriteLine("Subtracting two values: " + sub(10,4));

Open in new window

source code;
simple-delegate-sample.zip
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
systanCommented:
sample2;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace delegatetest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private delegate void delegatelocalrec(string test);
        private delegatelocalrec delegatetest = new delegatelocalrec(procedureorfunction); 
        
        private static void procedureorfunction(string test)
        {
            MessageBox.Show (test);
        }

        //NOTICE here, the messagebox called from procedureorfunction never paused
        private void button1_Click(object sender, EventArgs e)
        {
            delegatetest.BeginInvoke("Delegate Called By Button Event, can be called MultiThreading", null, null);
            MessageBox.Show("Direct called");
        }

    }
}

Open in new window

source code;
delegatesample2.zip
0
systanCommented:
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

systanCommented:
>>delegate syntax
there are many ways to use the delegate, the syntax is many as you can see the samples and links.

>>and why should I need a delegate?
it can produce multithreading method
if your application needs real data processing
0
systanCommented:
Are_you_not_satisfied?
0
systanCommented:
Do you need something else?
Here's another one;
http://www.brainbell.com/tutors/C_Sharp/Delegates_and_Event_Handlers.htm
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.