-2

If you write a class and make it implement IEnumerable<out T>, you will find one method is implicit, while the other is an explicit. But our own interfaces cannot do that, so how to make our own interfaces (some methods are implicit and some explicit?)


The detailled steps are:

1) Create a class.

2) Make the class implement from "IEnumerable".

3) Choose "Implicit From the interface".

For step 3, you will find that two methods are generated:

enter image description here

I wonder to know:

1) Why when I choose choice 1, one method is implicit while the other is exclipit?

2) What can I do to make the Vs's intellisens act the same result, what kinds of interfaces can I define? Can you give me some samples?

Regurads!

xqMogvKW
  • 628
  • 7
  • 17
  • 1
    `IEnumerable` derives from `IEnumerable` meaning that every class implementing `IEnumerable` has to implement two methods with the same signature `GetEnumerator()`. The only way you can do that is by explicitly implementing one of them - `IEnumerable.GetEnumerator()` – chomba May 28 '15 at 01:07
  • @chomba: I know the reason, but What I want to know is: How can I define an interface with some methods, and another class implements it, some methods are implicit, while the others are exclipit (I mean just Vs will insense me, just like what actions from IEnumerable )? – xqMogvKW May 28 '15 at 03:46
  • See my answer below. – chomba May 28 '15 at 05:08

2 Answers2

1

All interface method implementations can be implemented explicitly or implicitly (see here).

The reason IEnumerable<out T> needs the GetEnumerator method to be explicitly implemented is that IEnumerable<out T> derives from IEnumerable and both interfaces have different same named methods.

Community
  • 1
  • 1
Eli Algranti
  • 8,707
  • 2
  • 42
  • 50
0

What can I do to make the Vs's intellisens act the same result, what kinds of interfaces can I define? Can you give me some samples?

Let's say you have these two interfaces:

public interface IContainer<out T> : IContainer
{
    new T Value { get; }
}

public interface IContainer
{
    object Value { get; }
}

Let's now define a StringContainer class that implements IContainer<string>:

public class StringContainer : IContainer<string> { }

If you hover over IContainer<string> you should see the same tooltip that shows up when implementing IEnumerable<T>.

If you click the first option - Implement Interface, Visual Studio will realize that it can't implicitly implement both members(IContainer.Value and IContainer<string>.Value) because they have the same signature.

So, VS will implicitly implement the member declared in the interface being implemented (that is IContainer<string>.Value) and will explicitly implement the inherited interface member IEnumerable.Value.

chomba
  • 1,390
  • 11
  • 13