Wednesday, July 24, 2013

C#: using Delegates

     Now I want to code in a not-so-noob way.  People have been talking about 'delegates' and I'm like, 'Hey, I don't wanna feel left out.'  For starters, I have to settle on this simple understanding that a delegate is used to reference a method.

     A simple example;

namespace Delegate
{
    public delegate int DelegateMethod(int x, int y);
   
    public static void Main()
    {
        DelegateMethod addMethod = new DelegateMethod(Calculator.Add);
        int resultAdd = addMethod(10, 15);
    }
   
    public class Calculator
    {
        public static int Add(int x, int y)
        {
            return x + y;
        }
    }
}

No comments:

Post a Comment