了解代表逆向有用性

时间:2013-09-12 19:43:00

标签: c# generics delegates variance

所以我有一个委托定义为:

public delegate void MyDelegate<T>(T myParameter);

Resharper建议我按照以下方式制作T逆变器:

public delegate void MyDelegate<in T>(T myParameter);

现在,我很难理解这有什么用处?我知道它阻止我使T成为一种返回类型,但除此之外,通过使T逆变而得到什么有用的约束?也就是说,当需要将委托与实例一起使用时,我可以使用

创建哪些实例
public delegate void MyDelegate<T>(T myParameter);

我无法使用

创建
public delegate void MyDelegate<in T>(T myParameter);

3 个答案:

答案 0 :(得分:6)

以下是一个实例,如果删除逆变in标记,它将无法编译:

delegate void Callback<in T>(T t);

public Form1()
{
    InitializeComponent();
    Callback<Control> showText = control => MessageBox.Show(control.Text);
    var button = new Button();
    AddButtonClickCallback(button, showText);
    var label = new Label();
    AddLabelClickCallback(label, showText);
}

static void AddButtonClickCallback(Button button, Callback<Button> callback)
{
    button.Click += delegate { callback(button); };
}

static void AddLabelClickCallback(Label label, Callback<Label> callback)
{
    label.Click += delegate { callback(label); };
}

有点做作,当然,但至少应该让你知道没有它你不能做的事情。

特别要考虑AddLabelClickCallbackAddButtonClickCallback是否为库函数,Callback是否为库委托。如果它是在没有逆变的情况下定义的,那么你必须定义不同的代理showButtonTextshowLabelText,即使你只是希望他们做同样的事情。

答案 1 :(得分:4)

泛型上的in关键字允许进行隐式转换。基本上,您可以为委托分配较少的派生委托类型...并不总是有用,请阅读此处以获取更多信息。

http://msdn.microsoft.com/en-us/library/dd469484.aspx

示例,来自MSDN文章:

// Contravariant delegate. 
public delegate void DContravariant<in A>(A argument);

// Methods that match the delegate signature. 
public static void SampleControl(Control control)
{ }
public static void SampleButton(Button button)
{ }

public void Test()
{
    // Instantiating the delegates with the methods.
    DContravariant<Control> dControl = SampleControl;
    DContravariant<Button> dButton = SampleButton;

    // You can assign dControl to dButton 
    // because the DContravariant delegate is contravariant.
    dButton = dControl;

    // Invoke the delegate.
    dButton(new Button()); 
}

在该示例中,Control将隐式转换为Button类型,但如果Button中未定义隐式转换为Button类型,则可能会发生异常}}允许Control成为Button

有关隐式转化定义的更多信息:http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx

答案 2 :(得分:1)

在stackoverflow上已经多次询问过这个问题: Covariance and contravariance in programming languages

我建议你通过: http://blogs.msdn.com/b/ericlippert/archive/2007/10/22/covariance-and-contravariance-in-c-part-four-real-delegate-variance.aspx

Covariance and Contravariance in C#, Part One
Covariance and Contravariance in C#, Part Two: Array Covariance
Covariance and Contravariance in C#, Part Three: Method Group Conversion Variance
Covariance and Contravariance in C#, Part Four: Real Delegate Variance
Covariance and Contravariance In C#, Part Five: Higher Order Functions Hurt My Brain
Covariance and Contravariance in C#, Part Six: Interface Variance
Covariance and Contravariance in C# Part Seven: Why Do We Need A Syntax At All?
Covariance and Contravariance in C#, Part Eight: Syntax Options
Covariance and Contravariance in C#, Part Nine: Breaking Changes
Covariance and Contravariance in C#, Part Ten: Dealing With Ambiguity