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?
 
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
 
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.