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

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...
http://www.code-sample.com
http://www.code-sample.xyz

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.