Delegates

Recently, I have started working with C# on a project at Disney. This is my first experience using C# professionally and I will start collecting some notes on C# on my blog.

1. Delegates




Code Example ( Unity )  :


using UnityEngine;
using System.Collections;

public class DelegateExample : MonoBehaviour {

    // Define and Declare
    delegate void iAmDelegate();
    iAmDelegate delegateOne;


    void MethodForCallback(iAmDelegate callbackFunction)
    {
        callbackFunction ();
    }


    void callbackOne()
    {
        Debug.Log ("callback one");
    }


    void callbackTwo()
    {
        Debug.Log ("callback two");
    }


    void Start () {

        // Assign
        delegateOne = callbackOne;

        // Pass delegate in CallbackFunction
        MethodForCallback(delegateOne);
    }

    void Update () {
    
    }
}








Comments

Popular posts from this blog

Bounding Box in PIL (Python Image Library)

Dictionary vs KeyValuePair vs Struct - C#

Rendering order and Z-sorting