我对C#接口成员的默认访问修饰符感到困惑

我对C#接口成员的默认访问修饰符感到困惑

本文介绍了我对C#接口成员的默认访问修饰符感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

interface 方法的访问修饰符是什么?它应该是 public protected ,因为您在实现它们时可以访问它们(这是有道理的)。它也应该是 abstract ,因为它们没有实现。但是最近我一直在读一本名为 CLR Via C#的书,关于接口的章节说明如下

What is the access modifier of interface methods? It should be public or protected because you have access to them when you implement them (which makes sense). It also should be abstract because they don't have implementation. But lately I've been reading a book called CLR Via C# and the chapter about interfaces says the following

当您标记界面时 member virtual 编译器抱怨访问修饰符无效。我的意思是没有访问修饰符对 interface 中的任何内容有效,而不是编译器给它们的默认值?有人能说清楚吗?

When you mark the interface member virtual compiler complains that the access modifier in not valid. I mean no access modifier is valid for anything in interface rather than the default one which is given to them by compiler right? Can anyone make it clear for me?

推荐答案

关于接口成员的实现:

因此,您无法使用 protected 一个实现接口成员。

So you can't implement an interface member using protected one.

这篇关于我对C#接口成员的默认访问修饰符感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:44