What is it called when editing the interface?

I am browsing the LitJSON library. There are many segments in the code, for example

public class JsonData : IJsonWrapper, IEquatable<JsonData> #region ICollection Properties int ICollection.Count { get { return Count; } } #end region 

For the method, I know how overriding / overloading works, but in the example above the code reads: int ICollection.Count

I am not familiar with this format for signing a method. Is the encoder trying to explicitly specify its ICollection.Count interface?

Could you explain what this is called? (is this still overriding?).

+3
json override c # interface ienumerable
source share
2 answers

It is called an explicit interface implementation . It is mainly used to disambiguate members with the same name present in different interfaces, which also need different implementations.

You have

 interface ISomething1 { void DoSomething(); } interface ISomething2 { void DoSomething(); } class MyClass : ISomething1, ISomething2 { void ISomething1.DoSomething() { //Do something } void ISomething2.DoSomething() { //Do something else } } 

Without the implementation of the Explicit interface, you cannot provide a different implementation of DoSomething for both interfaces that we implement.

If you want to implement any interface and want to hide it from the client (to some extent), you can use an explicit implementation. Array class implements the IList interface explicitly and that it hides IList.Add , IList.Remove , etc. However, you can call it if you apply it to the IList type. But in this case you will get an exception.

Members implemented through explicit implementations are not displayed through an instance of the class (even inside the class). You need to access it through an instance of the interface.

 MyClass c = new MyClass(); c.DoSomething();//This won't compile ISomething1 s1 = c; s1.DoSomething();//Calls ISomething1 version of DoSomething ISomething2 s2 = c; s2.DoSomething();//Calls ISomething2 version of DoSomething 
+12
source share

What is called Explicit Interface Implementation . It is used to display properties only for instances of the specified interface.

The above example can only display the Count property if the declared variable is of type ICollection

MSDN


Example

Here is a good example of use, think about playing blackjack, both the Player and the Dealer will be dealt two cards.

The dealer will show only one card of his hand, while you can see them, so our hand needs a different behavior, depending on the interface specified by the client.

 public interface IHand { List<Card> CurrentHand { get; } } public interface IDealerHand : IHand { } public interface IPlayerHand : IHand { } public class Hand : IDealerHand, IPlayerHand{ private List<Card> cardsHeld; // The implementation here will ensure that only a single card for the dealer is shown. List<Card> IDealerHand.CurrentHand { get { return cardsHeld.Take(1); } } // The implementation here will ensure that all cards are exposed. List<Card> IPlayerHand.CurrentHand { get { return cardsHeld; } } } 
+3
source share

All Articles