C# Explicit Interface Implementation when implementing two interfaces, both with the same method and different implementations
Questions tagged [explicit-interface]
88 questions
4
votes
1 answer
How do I work with explicit interface events?
So I made a few interfaces like this:
public interface IDrawActions : ISimpleDrawable
{
Action PreDrawAction { get; set; }
Action PostDrawAction { get; set; }
event EventHandler PreDrawActionChanged;
event…

Kyle Baran
- 1,793
- 2
- 15
- 30
3
votes
1 answer
Why does List declare GetEnumerator() and IEnumerable.GetEnumerator()?
Why does List define these three methods?
public Enumerator GetEnumerator()
=> new Enumerator(this);
IEnumerator IEnumerable.GetEnumerator()
=> new Enumerator(this);
IEnumerator IEnumerable.GetEnumerator()
…

David Klempfner
- 8,700
- 20
- 73
- 153
3
votes
4 answers
How to call a base function that's an explicit interface implementation?
Basic problem (pseudocode):
interface ISomethingDoer
{
void DoSomething();
}
class A: ISomethingDoer
{
void ISomethingDoer.DoSomething()
{
Something.Do();
}
}
class B: A, ISomethingDoer
{
void ISomethingDoer.DoSomething()
…

Mason Wheeler
- 82,511
- 50
- 270
- 477
3
votes
1 answer
How do I call an explicit interface method on a base class in C#?
C# has a useful explicit interface feature that lets you create methods that implement interfaces while avoiding potential name conflicts.
public abstract class BaseClass : IDisposable {
public int Value;
void IDisposable.Dispose() => Value =…

benjamin.popp
- 547
- 1
- 6
- 20
3
votes
2 answers
Cannot retrieve explicit interface implemented member
I'm using Roslyn to analyze C# code and I've run into an issue when playing around with explicitly implemented interfaces. Given a type that implements an interface, I'm unable to retrieve explicitly implemented members by name. For example:
var…

JoshVarty
- 9,066
- 4
- 52
- 80
3
votes
2 answers
Optional parameters in explicitly implemented interfaces
public interface IFoo
{
void Foo(bool flag = true);
}
public class Test : IFoo
{
void IFoo.Foo(bool flag = true) //here compiler generates a warning
{
}
}
The warning says that the given default value will be ignored as it is used…

horgh
- 17,918
- 22
- 68
- 123
3
votes
2 answers
Single fortran module in multiple files
I just read the very good question/answers here about proper ways to use modules in Fortran. By writing subroutines in modules, one makes them explicit, in addition to clarifying the code.
To my knowledge, a module has to be put in a single file,…

max
- 563
- 4
- 17
2
votes
1 answer
Why does List use an explicit interface method implementation to implement non-generic interface methods?
// Some interface method signature
public interface IList : ICollection {
...
bool Contains(Object value);
...
}
public interface IList : ICollection { ... }
public interface ICollection : IEnumerable {
...
bool…
user9623401
2
votes
1 answer
Is there a way to initialize EII property within constructor?
Let's start with code from MS page:
interface ILeft
{
int P { get;}
}
interface IRight
{
int P();
}
class Middle : ILeft, IRight
{
public int P() { return 0; }
int ILeft.P { get { return 0; } }
}
I would like to change Middle in…

astrowalker
- 3,123
- 3
- 21
- 40
2
votes
1 answer
Distinguishing generics in Fortran by other than type/kind/rank
I make huge use of non-1 indexed ALLOCATABLE arrays, whose actual lower (and thus upper) bounds I want to be known for the procedures they're given to as IN/INOUT args (so I declare these dummy arguments as deferred-shape arrays to make them be…

Enlico
- 23,259
- 6
- 48
- 102
2
votes
1 answer
Indexer - Explicit Interface Member Implementation
I would need an working example-program of an Explicit Interface Member Implementation of an Indexer.
Microsofts c# docu of Indexers states that this is possible, but does not provide a working example (at least for me the example is not working). I…

Ini
- 548
- 7
- 19
2
votes
1 answer
Is it possible to inject into explicitly implemented method using Ninject?
Here is what I would like to do:
public interface IInject
{
[Inject]
void Inject(T reference);
}
private class Foo : IInject
{
public Bar Bar { get; private set; }
void IInject.Inject(Bar reference)
{
…

Johan Larsson
- 17,112
- 9
- 74
- 88
2
votes
2 answers
Why do I need to cast 'this' to interface type in a C# explicit implementation?
I have an interface:
public interface Profile
{
string Name { get; }
string Alias { get; set; }
}
All objects that implement Profile have a Name and an Alias, but some restrict Alias such that it's always the same as Name. The ones that…

jhabbott
- 18,461
- 9
- 58
- 95
2
votes
3 answers
How costly is boxing when explicitly implementing an interface
The current guidlelines for explicit member implementation recommend:
Using explicit members to approximate private interface implementations. If you need to implement an interface for only infrastructure reasons and you never expect developers to…

Oppositional
- 11,141
- 6
- 50
- 63
2
votes
2 answers
How are explicit interface implementations implemented in IL?
I've been having a look at explicit interface implementations in IL. The method Method in the following class (interface IA has a single Method() on it):
public class B : IA
object IA.Method() {
/* code */
}
}
is compiled to the…

thecoop
- 45,220
- 19
- 132
- 189