开发者

Questions on delegate functions in C#?

I'm learning about C# and one of the areas that interested me were delegate functions. The following code uses them to apply a function to each number in a range [start, limit[ before it is summed. This is a fairly simple example for clarity.

// function passing a delegate function
public override void solveProblem() {
    int squareOfSum = (int) Math.Pow(Library.Math.sumRange(1, 101), 2);
    int sumOfSquares = Library.Math.sumRange(1, 101, new Func<double, double, double>(Math.Pow), 2);

    this.solution = (squareOfSum - sumOfSquares).ToString();
}

// function that accepts / uses delegate function
static public int sumRange(int start, int limit, Delegate operation, params object[] args) {
    int sum = 0;

    for (int current = start; current < limit; current++) {
        // add the current number in the range as an arguement
        object[] newArgs = new object[args.Length + 1];
        newArgs[0] = current;
        args.CopyTo(newArgs, 1);

        // sum the result of the operation (delegate function)
        sum += Convert.ToInt32(operation.DynamicInvoke(newArgs));
    }

    return sum;
}

The specific questions I have are:

  • Is it possible to use dynamic delegate functions (that accept a variable length list of parameters with unkown types) but force the delegate function to return a specific type? With non-dynamic delegate functions you set a return type, but also have to set the number of parameters and their types.

  • How much slower is using DynamicInvoke than using a non-dynamic delegate function?

  • What is the best way to handle parameters than what I currently do (which is accept a list of other parameters and prepend any parameters the function that uses the delegate needs to add in)?

  • Do I need to declare 'new Func(Math.Pow)' to pass in the power function, or is there a way to just pass Math.Pow (and have the return type and parameters be passed implicitly)?

I've looked at the C# docs and this StackOverflow question to learn how to use delegate functions, I just want to learn more about them.

T开发者_JAVA百科hanks!

jtfairbank


Is it possible to use dynamic delegate functions (that accept a variable length list of parameters with unkown types) but force the delegate function to return a specific type? With non-dynamic delegate functions you set a return type, but also have to set the number of parameters and their types.

You don't "force" the function to return something. The function returns something or it doesn't return something. The function "forces" you to accept something :-) (or to ignore it)

You can call a dynamic delegate through the DynamicInvoke and then cast the return value to what you know your delegate returns (or you keep it as an object). I say "what you know your delegate returns" but the reality is a little more complex: for Value Types you have to cast the return value to precisely the type used as return value or you'll get InvalidCastException. For reference types you can use an interface or a base class of the object returned (with some exceptions for Nullable types)

How much slower is using DynamicInvoke than using a non-dynamic delegate function?

I've tested a void Do() method (the most simple method possible, with no boxing for parameters) and the time difference was demential. Let's say 400x :-) On http://ideone.com/f34cj it's between 70x and 150x.

Do I need to declare 'new Func(Math.Pow)' to pass in the power function, or is there a way to just pass Math.Pow (and have the return type and parameters be passed implicitly)?

Creating a new Func/Action delegate is the right path.


In general the right solution is not what you are doing. The right solution is do as LINQ does. For example:

int pow = 2;
Func<int, int> myFunc = p => (int)Math.Pow(p, pow);

var listOfNumbers = Enumerable.Range(1, 100);

var result = listOfNumbers.Sum(p => myFunc(p));

Now, you have a delegate (myFunc) that takes a number and returns the square of that number (and note that through "closures" it closes around pow) (if you don't know what a closure is, try putting that word in google with the words lambda function). A list of numbers as an IEnumerable<int> and you do the sum of these numbers "converted" by myFunc.


Read on LINQ - usage of IEnumerable produces much more compact code.

I.e. combination of Enumrable.Range ( http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx ) and Enumerable.Aggregate ( http://msdn.microsoft.com/en-us/library/bb548651.aspx ) is exactly what you trying to achieve:

var sum = Enumerable.Range(start,limit).Aggregate((s, cur)=> s+cur);
var sumSq = Enumerable.Range(start,limit).Aggregate((s, cur)=> s+ cur * cur);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜