C# 中接口成员的访问修饰符

发布于 2024-07-25 15:28:01 字数 365 浏览 11 评论 0原文

我从以下属性中收到编译错误。
错误是:

“修饰符‘public’对此项目无效”

public System.Collections.Specialized.StringDictionary IWorkItemControl.Properties
{
    get { return properties; }
    set { properties = value; }
}

但如果我删除 IWorkItemControl 它编译正常。

为什么我会收到此错误?签名中包含/不包含接口名称有什么区别?

I am getting a compile error from the following property.
The error is:

"The modifier 'public' is not valid for this item"

public System.Collections.Specialized.StringDictionary IWorkItemControl.Properties
{
    get { return properties; }
    set { properties = value; }
}

but if I remove the IWorkItemControl it compiles fine.

Why am I getting this error and what is the difference of having / not having the interface name in the signature?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一腔孤↑勇 2024-08-01 15:28:01

显式接口实现不允许您指定任何访问修饰符。 当您显式实现接口成员时(通过在成员名称之前指定接口名称),您可以仅使用该接口访问该成员。 基本上,如果您这样做:

System.Collections.Specialized.StringDictionary IWorkItemControl.Properties
{
    get { return properties; }
    set { properties = value; }
}

您不能这样做:

MyClass x = new MyClass();
var test = x.Properties; // fails to compile
// You should do:
var test = ((IWorkItemControl)x).Properties; // accessible through the interface

EII 有多种用例。 例如,您希望为您的类提供一个 Close 方法来释放获取的资源,但您仍然希望实现 IDisposable。 你可以这样做:

class Test : IDisposable {
    public void Close() {
        // Frees up resources
    }
    void IDisposable.Dispose() {
        Close();
    }
}

这样,类的使用者只能直接调用 Close (他们甚至不会在 Intellisense 列表中看到 Dispose),但你仍然可以使用 < code>Test 类中任何需要 IDisposable 的地方(例如在 using 语句中)。

EII 的另一个用例是为两个接口提供同名接口成员的不同实现:

interface IOne {
   bool Property { get; }
}

interface ITwo {
   string Property { get; }
}

class Test : IOne, ITwo {
   bool IOne.Property { ... }
   string ITwo.Property { ... }
}

如您所见,如果没有 EII,甚至不可能在单个类中实现此示例的两个接口(如属性仅在返回类型上有所不同)。 在其他情况下,您可能希望通过不同的接口有意为类的各个视图提供不同的行为。

Explicit interface implementation does not let you specify any access modifiers. When you implement an interface member explicitly (by specifying the interface name before the member name), you can access that member only using that interface. Basically, if you do:

System.Collections.Specialized.StringDictionary IWorkItemControl.Properties
{
    get { return properties; }
    set { properties = value; }
}

You can't do:

MyClass x = new MyClass();
var test = x.Properties; // fails to compile
// You should do:
var test = ((IWorkItemControl)x).Properties; // accessible through the interface

There are several use cases for EII. For example, you want to provide a Close method for your class to free up acquired resources but you still want to implement IDisposable. You could do:

class Test : IDisposable {
    public void Close() {
        // Frees up resources
    }
    void IDisposable.Dispose() {
        Close();
    }
}

This way, the consumers of class can only call Close directly (and they won't even see Dispose in Intellisense list) but you can still use the Test class wherever an IDisposable is expected (e.g. in a using statement).

Another use case for EII is providing different implementations of an identically named interface member for two interfaces:

interface IOne {
   bool Property { get; }
}

interface ITwo {
   string Property { get; }
}

class Test : IOne, ITwo {
   bool IOne.Property { ... }
   string ITwo.Property { ... }
}

As you see, without EII it's not even possible to implement both interfaces of this example in a single class (as the properties differ just in return type). In other cases, you might want to intentionally provide different behavior for individual views of a class through different interfaces.

小矜持 2024-08-01 15:28:01

接口的所有元素都必须是公共的。
毕竟,接口是对象的公共视图。

由于 Properties 是接口 IWorkItemControl 的一个元素,因此它已经是公共的,您无法指定其访问级别,甚至无法冗余地指定它是公共的。

All elements of an interface must be public.
After all, an interface is the public view of an object.

Since Properties is an element of an interface IWorkItemControl, it is already public, and you cannot specify its access level, even to redundantly specify that it is public.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文