简体   繁体   English

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

[英]Access modifiers on interface members in C#

I am getting a compile error from the following property. 我从以下属性收到编译错误。
The error is: 错误是:

"The modifier 'public' is not valid for this item" “修饰符'public'对此项无效”

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

but if I remove the IWorkItemControl it compiles fine. 但如果我删除IWorkItemControl它编译得很好。

Why am I getting this error and what is the difference of having / not having the interface name in the signature? 为什么我收到此错误,签名中是否有/没有接口名称有什么区别?

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. 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 . 例如,您希望为类提供Close方法以释放已获取的资源,但您仍希望实现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 (eg in a using statement). 这样,类的使用者只能直接调用Close (他们甚至不会在Intellisense列表中看到Dispose ),但是你仍然可以在任何需要IDisposable地方使用Test类(例如在using语句中)。

Another use case for EII is providing different implementations of an identically named interface member for two interfaces: EII的另一个用例是为两个接口提供同名的接口成员的不同实现:

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). 如您所见,没有EII, 甚至不可能在单个类中实现此示例的两个接口(因为属性在返回类型上不同)。 In other cases, you might want to intentionally provide different behavior for individual views of a class through different interfaces. 在其他情况下,您可能希望通过不同的接口有意为类的各个视图提供不同的行为。

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. 由于PropertiesIWorkItemControl接口的一个元素,因此它已经公开,您无法指定其访问级别,甚至可以冗余地指定它是公共的。

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

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