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.Collecti ons.Generic;
using System.Text;
public delegate double Delegate_Prod(i nt a, int b);
class Class1
{
static double fn_Prodvalues(i nt val1, int val2)
{
return val1 * val2;
}
static void Main(string[] args)
{
//Creating the Delegate Instance
Delegate_Prod delObj = new Delegate_Prod(f n_Prodvalues);
Console.Write(" Please Enter Values");
int v1 = Int32.Parse(Con sole.ReadLine() );
int v2 = Int32.Parse(Con sole.ReadLine() );
//use a delegate for processing
double res = delObj(v1, v2);
Console.WriteLi ne("Result :" + res);
Console.ReadLin e();
}
}
*************** *************** *************** *****
I understand why this works. But why would you do this if you can just do:
*************** *************** *************** *******
using System;
using System.Collecti ons.Generic;
using System.Text;
public delegate double Delegate_Prod(i nt a, int b);
class Class1
{
static double fn_Prodvalues(i nt val1, int val2)
{
return val1 * val2;
}
static void Main(string[] args)
{
//Creating the Delegate Instance
Delegate_Prod delObj = new Delegate_Prod(f n_Prodvalues);
Console.Write(" Please Enter Values");
int v1 = Int32.Parse(Con sole.ReadLine() );
int v2 = Int32.Parse(Con sole.ReadLine() );
//use a delegate for processing
double res = fn_Prodvalues(v 1, v2); <---------
Console.WriteLi ne("Result :" + res);
Console.ReadLin e();
}
}
*************** *************** *************** *******
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
For example:
If I do something like:
*************** *************** *************** *****
using System;
using System.Collecti ons.Generic;
using System.Text;
public delegate double Delegate_Prod(i nt a, int b);
class Class1
{
static double fn_Prodvalues(i nt val1, int val2)
{
return val1 * val2;
}
static void Main(string[] args)
{
//Creating the Delegate Instance
Delegate_Prod delObj = new Delegate_Prod(f n_Prodvalues);
Console.Write(" Please Enter Values");
int v1 = Int32.Parse(Con sole.ReadLine() );
int v2 = Int32.Parse(Con sole.ReadLine() );
//use a delegate for processing
double res = delObj(v1, v2);
Console.WriteLi ne("Result :" + res);
Console.ReadLin e();
}
}
*************** *************** *************** *****
I understand why this works. But why would you do this if you can just do:
*************** *************** *************** *******
using System;
using System.Collecti ons.Generic;
using System.Text;
public delegate double Delegate_Prod(i nt a, int b);
class Class1
{
static double fn_Prodvalues(i nt val1, int val2)
{
return val1 * val2;
}
static void Main(string[] args)
{
//Creating the Delegate Instance
Delegate_Prod delObj = new Delegate_Prod(f n_Prodvalues);
Console.Write(" Please Enter Values");
int v1 = Int32.Parse(Con sole.ReadLine() );
int v2 = Int32.Parse(Con sole.ReadLine() );
//use a delegate for processing
double res = fn_Prodvalues(v 1, v2); <---------
Console.WriteLi ne("Result :" + res);
Console.ReadLin e();
}
}
*************** *************** *************** *******
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
Comment