Thursday, October 25, 2012

.NET delegates program examples


Program
Class. Cs

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

delegate int Arithop(int x,int y);
namespace DelegatesEx
{
 
    class MathOperation
    {
        public static int add(int a, int b)
        {
            return (a + b);
        }
        public static int sub(int a, int b)
        {
            return (a - b);
        }
    }
}

Program.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace DelegatesEx
{
  class Delegate
{
            public static void Main(String[] args)
        {
            Arithop operation1 = new Arithop(MathOperation.add);
            Arithop operation2 = new Arithop(MathOperation.sub);
            Console.WriteLine(" Enter two numbers to do arthmetic operations");
            int n = int.Parse(Console.ReadLine());
            int m = int.Parse(Console.ReadLine());
          
            int result1 = operation1(n,m);
            int result2 = operation2(n,m);

            Console.WriteLine("\nAddition of two numbers");
            Console.WriteLine("\nResult={0}", result1);
            Console.WriteLine("\nsubtraction of two numbers");
            Console.WriteLine("\nResult={0}", result2);
            Console.ReadLine();   
}
    }
}


No comments:

Post a Comment