I am still trying to find out why to use a delegate and when it is overkill.
For example:
If I do something like:
**************************************************
using System;
using System.Collections.Generic;
using System.Text;
public delegate double Delegate_Prod(int a, int b);
class Class1
{
static double fn_Prodvalues(int val1, int val2)
{
return val1 * val2;
}
static void Main(string[] args)
{
//Creating the Delegate Instance
Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
Console.Write("Please Enter Values");
int v1 = Int32.Parse(Console.ReadLine());
int v2 = Int32.Parse(Console.ReadLine());
//use a delegate for processing
double res = delObj(v1, v2);
Console.WriteLine("Result :" + res);
Console.ReadLine();
}
}
**************************************************
I understand why this works. But why would you do this if you can just do:
************************************************** **
using System;
using System.Collections.Generic;
using System.Text;
public delegate double Delegate_Prod(int a, int b);
class Class1
{
static double fn_Prodvalues(int val1, int val2)
{
return val1 * val2;
}
static void Main(string[] args)
{
//Creating the Delegate Instance
Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
Console.Write("Please Enter Values");
int v1 = Int32.Parse(Console.ReadLine());
int v2 = Int32.Parse(Console.ReadLine());
//use a delegate for processing
double res = fn_Prodvalues(v1, v2); <---------
Console.WriteLine("Result :" + res);
Console.ReadLine();
}
}
************************************************** **
Here I do exactly the same thing, except I call the function directly as
opposed to having to set up a delegate, create the instance and then call
it.
If I were looking at this 2nd example what would cause me to think that I
should use a delegate instead and do the 1st example.
I understand the mechanics, but I am trying to see why I should do one over
the other.
Thanks,
Tom