Questions tagged [explicit-implementation]

28 questions
36
votes
4 answers

C# Language Design: explicit interface implementation of an event

Small question about C# language design :)) If I had an interface like this: interface IFoo { int Value { get; set; } } It's possible to explicitly implement such interface using C# 3.0 auto-implemented properties: sealed class Foo : IFoo { int…
controlflow
  • 6,667
  • 1
  • 31
  • 55
31
votes
8 answers

How to call an explicitly implemented interface-method on the base class

I have a situation, where two classes (one deriving from the other) both implement the same interface explicitly: interface I { int M(); } class A : I { int I.M() { return 1; } } class B : A, I { int I.M() { return 2; } } From the derived…
M4N
  • 94,805
  • 45
  • 217
  • 260
29
votes
1 answer

How to emit explicit interface implementation using reflection.emit?

Observe the following simple source code: using System; using System.Linq.Expressions; using System.Reflection; using System.Reflection.Emit; namespace A { public static class Program { private const MethodAttributes ExplicitImplementation…
mark
  • 59,016
  • 79
  • 296
  • 580
19
votes
4 answers

Is the C# "explicit implementation" of the interface present in Java?

In C#, if you have two base interfaces with the same method (say, F()) you can use explicit implementation to perform different impl. for F(). This alloes you to differently treat the object, corresponding to the current point of view: as…
lmsasu
  • 7,459
  • 18
  • 79
  • 113
17
votes
1 answer

C#: Property overriding by specifying the interface explicitly

While attempting to override the explicit interface implementation of the ICollection.IsReadOnly property from the Collection class, I came across some documents stating that explicit interface member implementations cannot be overridden…
13
votes
5 answers

In C#, why do interface implementations have to implement a another version of a method explicitly?

Take this example: public interface IFoo { IFoo Bar(); } public class Foo : IFoo { public Foo Bar() { //... } IFoo IFoo.Bar() { return Bar(); } //Why is this necessary? } Why is the implicit implementation of IFoo…
Matt
  • 21,026
  • 18
  • 63
  • 115
11
votes
2 answers

How can I access an explicitly implemented method using reflection?

Usually, I access a method in reflection like this: class Foo { public void M () { var m = this.GetType ().GetMethod ("M"); m.Invoke(this, new object[] {}); // notice the pun } } However, this fails when M is an explicit…
mafu
  • 31,798
  • 42
  • 154
  • 247
9
votes
2 answers

Is it possible to implement property setter explicitly while having a getter publicly available?

When I define an interface that contains a write-only property: public interface IModuleScreenData { string Name { set; } } and attempt to (naively) implement it explicitly with an intention for the property to also have a publicly available…
Nikola Anusev
  • 6,940
  • 1
  • 30
  • 46
7
votes
2 answers

XML Comments -- How do you comment explicitly implemented interfaces properly?

Code: public interface IFoo { void Bar(); } public class FooClass : IFoo { /// ... /// //How do you reference the IFoo.Bar() method public void Bar() { } /// ...
myermian
  • 31,823
  • 24
  • 123
  • 215
6
votes
6 answers

.NET C# Explicit implementation of grandparent's interface method in the parent interface

That title's a mouthful, isn't it?... Here's what I'm trying to do: public interface IBar { void Bar(); } public interface IFoo: IBar { void Foo(); } public class FooImpl: IFoo { void IFoo.Foo() { /* works as expected */ } //void…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
5
votes
3 answers

Explicit C# interface implementation of interfaces that inherit from other interfaces

Consider the following three interfaces: interface IBaseInterface { event EventHandler SomeEvent; } interface IInterface1 : IBaseInterface { ... } interface IInterface2 : IBaseInterface { ... } Now consider the following class that…
anthony
  • 40,424
  • 5
  • 55
  • 128
4
votes
1 answer

why does the Array class implement the Ilist Interface Explicitly not Implicitly?

My target language is C# with .net framework . I want to know what is the point or the reason behind this topic ? any advice and suggestions would be highly Appreciated . EDIT why i asked this question ? because right now , some useful members of…
4
votes
4 answers

How does one choose whether to implement an interface or explicitly implement an interface?

There are two ways to implement an interface: interface IMyInterface { void Foo(); } class IImplementAnInterface : IMyInterface { public void Foo() { } } // var foo = new IImplementAnInterface(); // foo.Foo(); //Ok //…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
3
votes
2 answers

Why doesn't C# support explicitly implemented virtual methods?

Interface methods in C# can be implemented explicitly, so that their implementation is invoked when an instance is explicitly cast to the interface type. Why is this not also supported on virtual methods of classes? Although working around the…
Justin Aquadro
  • 2,280
  • 3
  • 21
  • 31
3
votes
1 answer

Use explicit interface implementations with a dynamic object

I'm experimenting with explicit implentations of interfaces. This is to strip the intellisense with methods which are not valid in the current context. Use /practical-applications-of-the-adaptive-interface-pattern-the-fluent-builder-context/ as…
MortenRøgenes
  • 676
  • 4
  • 12
1
2