局部方法和lambda中的重载 [英] Overloading in local methods and lambda

查看:51
本文介绍了局部方法和lambda中的重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static void Main() {
  void TestLocal() => TestLocal("arg");
  TestLocal("arg");
}
static void TestLocal(string argument) {
}

在此示例中,局部函数与另一个要重载的方法具有相同的名称.但是,此方法从此函数内部甚至在Main()内部都是不可见的.对于lambdas也是如此:

In this example, local function has the same name as another method, which I want to overload. But this method is invisible from inside of this function, and even from inside of Main(). The same is for lambdas:

static void Main() {
  Action TestLambda = () => TestLambda("arg");
}
static void TestLambda(string argument) {
}

我知道,为什么lambda隐藏外部方法-因为它是一个局部变量,并且局部变量始终以这种方式工作.但是我看不到局部函数隐藏并且不重载外部方法的任何原因-这对于减少局部范围内的参数数量,进行某种携带可能非常有用.

I understand, why lambda hides outer method - because it is a local variable, and local variables always work this way. But I don't see any reason for local functions hide and not overload outer methods - it could be very useful for reducing amout of arguments in local scope, doing some kind of carrying.

为什么局部函数会隐藏方法?

Why local functions hide methods?

我可以想象一个复杂的例子

I can imagine an example where it gets complicated

void Bar(short arg) {
  Console.WriteLine("short");
}
void Bar(byte arg) {
  Console.WriteLine("byte");
}
void Test() {
  void Bar(int arg) {
    Console.WriteLine("int");
  }
  Bar(0);
}

使用参数类型解析已经足够复杂了.如果他们将重载添加到本地方法中,那将是工作面试的又一项愚蠢的任务,而对于编译器制造商而言又是一项艰巨的任务.还有virtualnew方法...

It was already complicated enough with argument type resolution. If they added overloading to local methods, it would be one more stupid task for job interviews and one more huge task for compiler makers. And there are also virtual and new methods...

推荐答案

为什么局部函数会隐藏方法?

Why local functions hide methods?

基本上是在方法内部的声明空间中引入方法名称.在该声明空间中,名称​​ only 指的是本地方法...就像对本地变量一样.我认为这与引入方法中的名称始终有效的方式是合理的.

Basically it's introducing the method name into the declaration space inside the method. Within that declaration space, the name only refers to the local method... just as it does for a local variable. I think that's reasonably consistent with the way that names introduced into methods have always worked.

我个人还是建议不要尝试这样做,因为这会引起混乱,但是如果您确实需要引用类方法,则只需使其明确即可:

I'd personally advise against trying to do this anyway, as it'll cause confusion, but if you really need to refer to the class method, just make it explicit:

ClassName.TestLocal("arg");

认为可以解决的是,局部方法不能在它们之间重载.你不能写:

What I do think could be fixed is that local methods can't be overloaded between themselves. You can't write:

void Foo()
{
    void Method(int x) {}
    void Method(string y) {}
}

这是由于一个相关的原因-就方法重载而言,方法的声明空间不能两次包含相同的名称,而类的声明空间可以这样做.也许有关这方面的规则将会放宽...

This is for a related reason - a method's declaration space can't include the same name twice, whereas a class's declaration space can do so, in terms of method overloading. Maybe the rules will be loosened around this...

这篇关于局部方法和lambda中的重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆