C# 带接口的访问修饰符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16101673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:00:32  来源:igfitidea点击:

Access Modifiers with Interfaces

c#interface

提问by Saurabh Mahajan

I am new to .net C#. I am learning about interfaces.

我是 .net C# 的新手。我正在学习接口。

  1. Can we use access modifiers with interfaces, if so, which are valid?
  2. By default, are interfaces internal?
  3. Can we use access modifiers on interface members, if so, which are valid?
  1. 我们可以在接口中使用访问修饰符吗?如果可以,哪些是有效的?
  2. 默认情况下,接口是内部的吗?
  3. 我们可以在接口成员上使用访问修饰符吗?如果可以,哪些是有效的?

采纳答案by Habib

Since C# 8.0 you can have access modifier inside the interface. See this post C# 8 Interfaces: Public, Private, and Protected Members

从 C# 8.0 开始,您可以在接口内使用访问修饰符。请参阅这篇文章 C# 8 接口:公共、私有和受保护的成员



Before C# 8.0

C# 8.0 之前

You should see:

你应该看到:

Access Modifier - MSDN

访问修饰符 - MSDN

Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access.Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.

直接在命名空间中声明的接口可以声明为 public 或 internal,就像类和结构一样,接口默认为内部访问。接口成员始终是公共的,因为接口的目的是使其他类型能够访问类或结构。不能将访问修饰符应用于接口成员。

(For your questions)

(对于你的问题)

Can we use Access Modifier with Interface

我们可以在接口中使用访问修饰符吗

Yes, they can be declared as public or internal

是的,它们可以被声明为公共的或内部的

By default Are inteface internal

默认情况下是内部接口

Yes.

是的。

what about Access Modfiers with Inteface members.

带有 Inteface 成员的 Access Modfiers 怎么样?

They are public. No access modifiers can be applied to interface members.

他们是公开的。不能将访问修饰符应用于接口成员。

回答by Fabio Marcolini

1.you CAN use access modifier with interface. you can use any access modifier even private on interfaces as you can do in classes

1.你可以使用带有接口的访问修饰符。您可以像在类中一样在接口上使用任何访问修饰符,甚至是私有的

2.yes, by default interfaces are internal

2.是的,默认接口是内部的

3.interface member must be public

3.interface成员必须是public

well, you can't define a private interface directly in a namespace as you can't define a private class in the namespace, that's because a private interace/class can't be used by anyone. But you can declare a private interface/class inside another class

好吧,你不能直接在命名空间中定义私有接口,因为你不能在命名空间中定义私有类,那是因为私有接口/类不能被任何人使用。但是您可以在另一个类中声明一个私有接口/类

I mean you cant do this:

我的意思是你不能这样做:

namespace myNamespace{
    private interface MyInterface{...}
}

that's because MyInterface wouldn't be utilizable by any other class/interface

那是因为 MyInterface 不能被任何其他类/接口使用

but you can do

但你可以

namespace myNamespace{
    class MyClass{
        private interface MyInterface{...}
    }
}

in this case MyInterface may be used only by MyClass

在这种情况下 MyInterface 只能由 MyClass 使用