无法为具有较少特定参数类型的匿名方法分配委托

Can’t assign delegate an anonymous method with less specific parameter type(无法为具有较少特定参数类型的匿名方法分配委托)
本文介绍了无法为具有较少特定参数类型的匿名方法分配委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Program
{
    delegate void Srini(string param);

    static void Main(string[] args)
    {
        Srini sr = new Srini(PrintHello1);
        sr += new Srini(PrintHello2);      //case 2:      
        sr += new Srini(delegate(string o) { Console.WriteLine(o); });
        sr += new Srini(delegate(object o) { Console.WriteLine(o.ToString()); }); //case 4:
        sr += new Srini(delegate { Console.WriteLine("This line is accepted,though the method signature is not Comp"); });//case 5
        sr("Hello World");
        Console.Read();
    }       

    static void PrintHello1(string  param)
    {
        Console.WriteLine(param);
    }

    static void PrintHello2(object param)
    {
        Console.WriteLine(param);
    }
}

编译器没有抱怨案例 2(见评论),好吧,原因很简单,因为字符串继承自对象.同样,为什么它抱怨匿名方法类型(参见注释//case 4:)

Compiler doesn't complain about the case 2 (see the comment), well, the reason is straight forward since string inherits from object. Along the same lines, why is it complaining for anonymous method types (see the comment //case 4:) that

无法将匿名方法转换为委托类型DelegateTest.Program.Srini",因为参数类型与委托参数类型不匹配

Cannot convert anonymous method to delegate type 'DelegateTest.Program.Srini' because the parameter types do not match the delegate parameter types

在正常方法的情况下,它不会在哪里?还是我在比较苹果和橙子?另一种情况是为什么它接受不带参数的匿名方法?

where as in case of normal method it doesn't? Or am I comparing apples with oranges? Another case is why is it accepting anonymous method without parameters?

推荐答案

方法组转换支持变体(从 C# 2 开始,C# 1 中没有),匿名函数转换根本不支持.鉴于每当您编写匿名函数时,您可以编写适当的参数,为什么不直接这样做呢?据我所知,在那里允许差异没有任何好处,而且会使规则更难正确.(在规范中,方差最终变得相当复杂.)

Method group conversions support variance (as of C# 2 - they didn't in C# 1), anonymous function conversions simply don't. Given that whenever you write an anonymous function you can write the appropriate parameter, why not just do so? There would be no benefit in allowing variance there as far as I can see, and it would make the rules harder to get right. (Variance ends up being pretty complex in the spec.)

没有参数列表的匿名方法基本上与任何委托的参数列表兼容,只要它没有 out 参数.基本上这是一种说法,我不在乎参数."这是 lambda 表达式所没有的匿名方法的一个特性:)

An anonymous method without a parameter list is basically compatible with any delegate's parameter list so long as it doesn't have out parameters. Basically it's a way of saying, "I don't care about the parameters." It's the one feature of anonymous methods that lambda expressions don't have :)

这篇关于无法为具有较少特定参数类型的匿名方法分配委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)