简体   繁体   English

C#-为委托动作的out参数设置默认值

[英]C# - setting a default value for an out parameter of a delegate action

protected IteratorAdvance<int> Advance;

public delegate void IteratorAdvance<T2>(out T2 itemIndex);

public IteratorBase()
{
   Advance = delegate(out int i) { i++; };   // <--- here
}

Above is a snippet of a class that is a wrapper for a number of types of loop. 上面是一个类的摘要,它是许多循环类型的包装。 In the constructor overload below, the instantiator can specify the method by which the condition of continuing a for loop is determined and also how the control value of that loop is modified on each iteration. 在下面的构造函数重载中,实例化程序可以指定确定继续for循环的条件的方法,以及如何在每次迭代中修改该循环的控制值。

Obviously, the simplest and most likely use of this is to start at 0 and increment by 1 until the last index of the data source is reached. 显然,最简单,最可能的用法是从0开始并以1递增,直到到达数据源的最后一个索引为止。 To that end I've provided default methods and an initial value that are effectively (and in this case a very convoluted version of) 为此,我提供了有效的默认方法和初始值(在这种情况下为非常复杂的版本)

for(var i = 0; i < list.Count; i++)

Here's the constructor: 这是构造函数:

public IteratorBase(IterationStyle style, Func<int, T1, bool> complete, IteratorAdvance<int> advance,  bool onErrorAbort = true, int initialValue = 0)
{
    Style = style;
    InitialValue = initialValue;
    Complete = complete;
    Advance = advance;
    AbortOnError = onErrorAbort;
}

The problem is where I'm incrementing the control value, the compiler wants a default value for a variable (i) that is declared in that scope and because it is an out parameter, cannot accept a default value. 问题是我要增加控制值,编译器想要在该范围中声明的变量(i)的默认值,并且因为它是out参数,所以不能接受默认值。

Even if I move the body of the function to a separate method, the same problem exists. 即使我将函数的主体移到单独的方法上,也存在相同的问题。 I cannot initialise the out parameter prior to the for loop itself initialising it. 我无法在for循环本身对其进行初始化之前对其进行初始化。

case IterationStyle.For:
     for (controlValue = InitialValue; !Complete(controlValue, item); Advance(out controlValue))
     {
         action.TargetItem = item = dataSource[controlValue];
         if (!ActWrapper(action))
             return false;
     }
     break;

The only solution I can think of is to intialise the out parameter only on the first call of the method, which will involve using some kind of boolean external to the method. 我能想到的唯一解决方案是仅在方法的第一次调用时初始化out参数,这将涉及在方法外部使用某种布尔值。 I don't think I'm alone in preferring to avoid that sort of thing. 我不认为我一个人愿意避免这种事情。 And it turns out it that doesn't work either so I would have to make the control value global. 事实证明,它也不起作用,因此我必须将控制值设为全局。 Over my dead body. 在我的尸体上。

Usually, in this sort of situation it means your approach is wrong. 通常,在这种情况下,这意味着您的方法是错误的。 But I can't think of another way. 但是我想不出另一种方式。 I'm hoping you can. 我希望你能。 Or else tell me how I can either initialise an out parameter or give it a default value. 否则告诉我如何初始化out参数或为它提供默认值。

Just in case anyone else is having a bad brain day, here's the solution. 以防万一其他人的大脑状况不好,这是解决方案。 Use ref not out . 使用ref not out

 Advance = delegate(ref int i) { i++; };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM