静态和实例方法具有相同的名称? [英] Static and Instance methods with the same name?

查看:228
本文介绍了静态和实例方法具有相同的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类既具有静态和C#中的非静态接口。是有可能有一个静态和在一类的非静态方法具有相同名称和签名σ

I have a class with both a static and a non-static interface in C#. Is it possible to have a static and a non-static method in a class with the same name and signature?

我得到一个编译器错误,当我尝试这样做,但由于某些原因,我认为有办法做到这一点。我错了或者是有没有办法有静态和非静态方法在同一个班?

I get a compiler error when I try to do this, but for some reason I thought there was a way to do this. Am I wrong or is there no way to have both static and non-static methods in the same class?

如果这是不可能的,有没有实现这样的事情,可一般适用于任何情况的好办法?

If this is not possible, is there a good way to implement something like this that can be applied generically to any situation?

修改
从我收到的答复,很明显,有没有办法做到这一点。我会用不同的命名系统来解决这个问题。

EDIT
From the responses I've received, it's clear that there is no way to do this. I'm going with a different naming system to work around this problem.

推荐答案

没有你不行。的原因的限制是静态方法,也可以从非静态上下文称为无需prePEND类名(代替MyClass.MyStaticMethod所以MyStaticMethod()())。编译器不能告诉你正在寻找在你拥有的。

No you can't. The reason for the limitation is that static methods can also be called from non-static contexts without needing to prepend the class name (so MyStaticMethod() instead of MyClass.MyStaticMethod()). The compiler can't tell which you're looking for if you have both.

您可以按照相同的规则,方法重载静态的和非静态方法具有相同的名称,但不同的参数,他们只是不能有完全相同的签名。

You can have static and non-static methods with the same name, but different parameters following the same rules as method overloading, they just can't have exactly the same signature.

这篇关于静态和实例方法具有相同的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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