【发布时间】:2022-11-24 12:35:02
【问题描述】:
namespace PickOverload;
class Program {
delegate string Formatter( object o );
string Show( double a ) { return a.ToString(); }
string Show( int a ) { return a.ToString(); }
string Format( Formatter f, object o ) { return f( o ); }
void SelectArgument() {
// error CS1503: Argument 1: cannot convert from 'method group' to 'Program.Formatter'
Format( Show, 1.234 );
}
void SelectDelegate() {
// error CS0123: No overload for 'Show' matches delegate 'Program.Formatter
Formatter x = this.Show;
}
void Run() {
SelectArgument();
SelectDelegate();
}
static void Main( string[] args ) {
new Program().Run();
}
}
是否有 C# 语法用于选择重载的 Show 方法之一作为 Format 方法或委托的参数?
我不是在寻找上述示例的解决方案,而是在寻找为委托或方法参数选择多个重载方法之一的方法。
这里有同样的问题:
void Run() {
double f = 1.234;
Format( Show, f );
Formatter x = this.Show;
}
static void Main(string[] args ) {
new Program().Run();
}
【问题讨论】:
-
同样的问题:void Run() { double f = 1.234;格式(显示,f);格式化程序 x = this.Show; } static void Main( string[] args ) { new Program().Run(); }
-
请不要在 cmets 中添加更多信息。改为编辑您的问题。
标签: c#