Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is it called when you edit an interface?

I am going through a LitJSON library. In the code there are lots of segments like

 public class JsonData : IJsonWrapper, IEquatable<JsonData>

 #region ICollection Properties
            int ICollection.Count {
                get {
                    return Count;
                }
            }
    #end region

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

I'm not familiar with that format for a method signature. Is the coder trying to explicitly state its the ICollection.Count interface?

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

like image 670
Aggressor Avatar asked Aug 30 '14 17:08

Aggressor


People also ask

What is an interface?

An interface may refer to any of the following: 1. When referring to software, an interface is a program that allows a user to interact with the computer or another computer over a network.

What is the importance of a user interface?

User interfaces enable users to effectively control the computer or device they are interacting with. A successful user interface should be intuitive, efficient, and user-friendly.

What are the different types of user interface elements?

User interface elements usually fall into one of the following four categories: Input controls allow users to input information into the system. If you need your users to tell you what country they are in, for example, you’ll use an input control to let them do so.

Is “programming to an interface” a concept?

The word interface is right there, so that must be what an interface is. Then there’s a bunch of people who take a different stand. So they argue the originators of the “programming to an interface” rule never meant it as the interface concept in C#. It’s an object-oriented concept, they say.


2 Answers

It is called explicit interface implementation. Mainly used to disambiguate members with same name present in different interfaces which also needs different implementation.

Consider 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 Explicit interface implementation you will not be able to provide different implementation of DoSomething for both the interfaces we implement.

If you want to implement some interface and you want to hide it from the client(upto some extent) you can use explicit implementation. Array class implements IList interface explicitly and that's how it hides IList.Add, IList.Remove etc. Nevertheless you can call it if you cast it to IList type. But you'll end up getting an Exception in this case.

Members implemented via explicit implementations are not visible via class instance(Even inside the class). You need to access it via interface instance.

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
like image 55
Sriram Sakthivel Avatar answered Sep 19 '22 18:09

Sriram Sakthivel


That's what's called an Explicit Interface Implementation. It is used to expose properties only on instances of the specified interface.

The example you provided above would only expose the Count property if the declared variable happened to be an ICollection type

MSDN


Example

Here's a good use case, think about a Blackjack game, both the Player and Dealer will be dealt two cards.

The dealer will only reveal one card of his hand, whilst you'll be able to see both of yours, so our hand needs a different behaviour dependent on the Interface that the client has specified.

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; } }
}
like image 43
Aydin Avatar answered Sep 18 '22 18:09

Aydin