delegate in c# with example

Delegates in C# Example

The basic examples of Delegates using in C# .Net as given below,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace delegateExample
{
    public delegate int AddByDelegate(int var1, int var2);

    public class DelegateClass
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }
        static void Main(string[] args)
        {
            //Creating the Delegate Instance
            AddByDelegate delObject = new AddByDelegate(Add);

            Console.Write("Please enter value");
            int num1 = Int32.Parse(Console.ReadLine());
            int num2 = Int32.Parse(Console.ReadLine());

            // Here method Add is call.
            int Result = delObject(num1, num2);
            Console.WriteLine("Result :" + Result);
            Console.ReadLine();
        }
    }
}
//The Delegate - OutPut

Result : 30


ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Delegates in C# Example Delegates in C# Example Reviewed by Anil Singh on 12:33 AM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^