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);
//The Delegate - OutPut

Result : 30

About blogger..


Hey! I am Anil Singh. I am Active Blogger, Writer and Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...

Post a Comment Powered by Blogger.