Delegate help for Func<T, TResult> declaration

S

Schizoid Man

Hello,

I have the following piece of code that uses a one-dimensional root solver
for solve for a particular value. I've currently got this code working by
using a generic public delegate double FX(double x);.

The code looks like:

public static void Main (string[] args)
{
Bond james;
james = new Bond(0.10, 2, 5); // Create a bond - this
constructor forces yield and price of zero
james.BondPrice = 50; // Set the bond price
FX f = delegate(double x)
{
james.BondYield = x;
return (BondPricer.CalcBondPrice(james) - james.BondPrice);
};
double ans = NewtonMethod.DoSolve(f);
Console.WriteLine("Root: {0}", ans);
Console.ReadLine();
}

Could someone give me some tips on how I could convert this using the Func<>
delegate type? I'm interested in writing a couple of other solver classes
and want to keep the delegate declaration as generic as possible (since I'll
be using this to solve for a variety of functions).

Thanks.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello,

I have the following piece of code that uses a one-dimensional root solver
for solve for a particular value. I've currently got this code working by
using a generic public delegate double FX(double x);.

The code looks like:

 public static void Main (string[] args)
        {
            Bond james;
            james = new Bond(0.10, 2, 5);  // Create a bond - this
constructor forces yield and price of zero
            james.BondPrice = 50; // Set the bond price
            FX f = delegate(double x)
            {
                james.BondYield = x;
                return (BondPricer.CalcBondPrice(james) -james.BondPrice);
            };
            double ans = NewtonMethod.DoSolve(f);
            Console.WriteLine("Root: {0}", ans);
            Console.ReadLine();
        }

Could someone give me some tips on how I could convert this using the Func<>
delegate type? I'm interested in writing a couple of other solver classes
and want to keep the delegate declaration as generic as possible (since I'll
be using this to solve for a variety of functions).

Thanks.

your delegate definition is Func<T> but having T as double. I do not
see why you want to change it. you cannot use Func as it accept things
like string, which you have no use for.
 
S

Schizoid Man

Peter Duniho said:
[...]
Could someone give me some tips on how I could convert this using the
Func<> delegate type? I'm interested in writing a couple of other solver
classes and want to keep the delegate declaration as generic as possible
(since I'll be using this to solve for a variety of functions).

In this example, the use of Func<T> isn't about making the code
_generic_. It's simply about using a pre-existing type instead of
declaring one yourself.

You can replace "FX" with "Func<double>" in the code (in the DoSolve()
argument list, and the Main() method, and anywhere else it appears). That
will allow you to not have to declare the "FX" type.

Thanks, Peter. I just realized that I am on VS 2005, so not sure this will
work.
 
S

Schizoid Man

Peter Duniho said:
In this example, the use of Func<T> isn't about making the code
_generic_. It's simply about using a pre-existing type instead of
declaring one yourself.

You can replace "FX" with "Func<double>" in the code (in the DoSolve()
argument list, and the Main() method, and anywhere else it appears). That
will allow you to not have to declare the "FX" type.

Thanks, Peter. I managed to get the code working using the predefined
delegate types, though frankly I think it makes the code less legible.

I think I prefer to use my own non-generic type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top