简体   繁体   English

为什么在 C# 中将静态类型传入和传出接口方法是有效的?

[英]Why is it valid to pass static types in and out of interface methods in C#?

I noticed that the following code seems to compile just fine, when I would've expected multiple errors:我注意到以下代码似乎编译得很好,而我预计会出现多个错误:

public interface ITest
{
    Math Foo(MathF x, ref Console y);
}

Math, MathF, and Console are all static classes - is there any reason why this is valid, or is it just an oddity of the specification/compiler? Math、MathF 和 Console 都是静态类 - 有什么理由证明这是有效的,还是只是规范/编译器的奇怪之处? When attempting to implement the interface, you then receive an error (I guess that means you can make an interface that's impossible to implement, which is kinda cool)在尝试实现接口时,您会收到一个错误(我想这意味着您可以制作一个无法实现的接口,这有点酷)

What's more, I can go one worse:更重要的是,我可以做得更糟:

using System;

namespace StaticParams
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ITest.Bar(null);
        }

        public interface ITest
        {
            Math Foo(MathF x, ref Console y);

            static void Bar(Math x)
            {
                Baz(x);
            }

            static void Baz(Math x)
            {
                Console.WriteLine("Hello World" + x + "!"); // x is null so we can't do much with it
            }
        }
    }
}

Output:输出:

Hello World!

Tested in VS 2022, using both C# 8.0 + .NET Core 3.1, and C# 10.0 + .NET 6.0.4.在 VS 2022 中测试,同时使用 C# 8.0 + .NET Core 3.1 和 C# 10.0 + .NET 6.0.4。

is there any reason why this is valid, or is it just an oddity of the specification/compiler?有什么理由证明这是有效的,还是只是规范/编译器的奇怪之处?

Questions like this can be difficult to answer.像这样的问题可能很难回答。 The strict literal answer is because that's how C#'s grammar is defined.严格的字面答案是因为这就是 C# 语法的定义方式。

Quoting from https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/interfaces :引用https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/interfaces

interface_declaration
    : attributes? interface_modifier* 'partial'? 'interface'
      identifier variant_type_parameter_list? interface_base?
      type_parameter_constraints_clause* interface_body ';'?
    ;
interface_body
    : '{' interface_member_declaration* '}'
    ;
interface_member_declaration
    : interface_method_declaration
    | interface_property_declaration
    | interface_event_declaration
    | interface_indexer_declaration
    ;
interface_method_declaration
    : attributes? 'new'? return_type identifier type_parameter_list?
      '(' formal_parameter_list? ')' type_parameter_constraints_clause* ';'
    ;

The attributes, return_type, identifier, and formal_parameter_list of an interface method declaration have the same meaning as those of a method declaration in a class ( §14.6 ).接口方法声明的属性、return_type、标识符和formal_parameter_list 与类中方法声明的含义相同( 第14.6 节)。

From there you'll have to knock yourself out with this: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#146-methods从那里你将不得不用这个来敲自己: https ://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#146-methods


But of course that's not really an enlightening answer.但这当然不是一个真正有启发性的答案。 I think what you really want to know is "what is the wisdom in allowing insanity like this?".我认为您真正想知道的是“允许这种精神错乱的智慧是什么?”。 Which of course is an opinion-based question.这当然是一个基于意见的问题。 So I'll answer that with these opinions of mine:所以我会用我的这些观点来回答这个问题:

  • It would probably just make the language or its implementation more complex to prevent it at this level在这个级别上阻止它可能只会使语言或其实现更加复杂
  • It's already prevented at another level—it's impossible to implement that interface as you pointed out, so already nobody is able to write useful code like that它已经在另一个层面上被阻止了——正如你所指出的那样实现该接口是不可能的,所以已经没有人能够编写这样有用的代码
  • Why not allow it when there's already so much other insanity out there?当外面已经有这么多其他疯狂时,为什么不允许它呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM