Skip to content

Instantly share code, notes, and snippets.

@dnasca
Created March 17, 2015 06:17
Show Gist options
  • Save dnasca/fb03f88021f7ba2d724e to your computer and use it in GitHub Desktop.
Save dnasca/fb03f88021f7ba2d724e to your computer and use it in GitHub Desktop.
using System;
/* Basic Delegate Notes
*
* A delegate is a type safe function pointer. It holds a reference(pointer) to a function
*
* A delegate must have a return type
*
* The signature of the delegate MUST match the signature of the function that the delegate points to
*
* A delegate is similar to a class, you can create an instance of it
*
* When you create an instance of a delegate, you must pass in the function name as a parameter to the delegate constructor
*
* The delegate syntax is similar to a method, but with a delegate keyword
*
*/
public delegate void SayHelloDelegate(string Message);
public class Program
{
public static void Main()
{
SayHelloDelegate sayHello = new SayHelloDelegate(SayHello); /*instantiate delegate with reference sayHello, pass the function
that is to be used as the parameter */
sayHello("Hello from the SayHelloDelegate"); //the SayHello function can now be invoked using the object reference to the delegate
}
public static void SayHello(string stringMessage)
{
Console.WriteLine(stringMessage);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment