Agg*_*sor 3 c# ienumerable json overriding interface
我正在浏览LitJSON库.在代码中有很多段,如
public class JsonData : IJsonWrapper, IEquatable<JsonData>
#region ICollection Properties
int ICollection.Count {
get {
return Count;
}
}
#end region
Run Code Online (Sandbox Code Playgroud)
对于我知道覆盖/重载如何工作的方法,但在上面的示例中,代码为:int ICollection.Count
我不熟悉方法签名的格式.编码器是否试图明确声明其ICollection.Count接口?
你能解释一下这是什么"被称为"(它是否仍然覆盖?).
Sri*_*vel 12
它被称为显式接口实现.主要用于消除不同接口中存在同名的成员的歧义,这些接口也需要不同的实现.
考虑一下你
interface ISomething1
{
void DoSomething();
}
interface ISomething2
{
void DoSomething();
}
class MyClass : ISomething1, ISomething2
{
void ISomething1.DoSomething()
{
//Do something
}
void ISomething2.DoSomething()
{
//Do something else
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有显式接口实现,您将无法DoSomething为我们实现的两个接口提供不同的实现.
如果要实现某个接口,并且希望将其从客户端隐藏(在某种程度上),则可以使用显式实现.Arrayclass IList显式实现接口,这就是它隐藏的方式等.但是如果你将它转换为类型IList.Add,IList.Remove你可以调用它IList.但在这种情况下你最终会得到一个例外.
通过显式实现实现的成员不能通过类实例(甚至在类中)可见.您需要通过接口实例访问它.
MyClass c = new MyClass();
c.DoSomething();//This won't compile
ISomething1 s1 = c;
s1.DoSomething();//Calls ISomething1's version of DoSomething
ISomething2 s2 = c;
s2.DoSomething();//Calls ISomething2's version of DoSomething
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
528 次 |
| 最近记录: |