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


Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - and
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book