Questions tagged [explicit-interface]

C# Explicit Interface Implementation when implementing two interfaces, both with the same method and different implementations

88 questions
55
votes
5 answers

Why is it illegal to have a private setter on an explicit getter-only interface implementation?

I tend to favor explicit interface implementations over implicit ones, as I think programming against the interface as opposed to against an implementation, is generally preferable, plus when dealing with web-services it is often a necessity. That…
Abel
  • 56,041
  • 24
  • 146
  • 247
30
votes
9 answers

How do I use reflection to get properties explicitly implementing an interface?

More specifically, if I have: public class TempClass : TempInterface { int TempInterface.TempProperty { get; set; } int TempInterface.TempProperty2 { get; set; } public int TempProperty …
Dane O'Connor
  • 75,180
  • 37
  • 119
  • 173
24
votes
3 answers

Type parameter 'T' has the same name as the type parameter from outer type '...'

public abstract class EntityBase { ... } public interface IFoobar { void Foo(int x) where T : EntityBase, new(); } public interface IFoobar where T : EntityBase, new() { void Foo(int x); } public class Foobar :…
michael
  • 14,844
  • 28
  • 89
  • 177
17
votes
5 answers

Explicit interface implementation cannot be virtual

For the record, I've already seen this connect item but I can't really understand what would be the problem in supporting this. Say I have the following code: public interface IInterface { void Method(); } public class Base : IInterface { …
thekip
  • 3,660
  • 2
  • 21
  • 41
17
votes
4 answers

Why would a class implement IDisposable explicitly instead of implicitly?

I was using the FtpWebResponse class and didn't see a Dispose method. It turns out that the class implements IDisposable, but does so explicitly so that you must first cast your instance to IDisposable before calling Dispose: // response is an…
MCS
  • 22,113
  • 20
  • 62
  • 76
17
votes
2 answers

F# and interface-implemented members

I have a vexing error. type Animal = abstract member Name : string type Dog (name : string) = interface Animal with member this.Name : string = name let pluto = new Dog("Pluto") let name = pluto.Name The last line,…
16
votes
2 answers

Access modifiers on interface members in C#

I am getting a compile error from the following property. The error is: "The modifier 'public' is not valid for this item" public System.Collections.Specialized.StringDictionary IWorkItemControl.Properties { get { return properties; } set…
benPearce
  • 37,735
  • 14
  • 62
  • 96
15
votes
3 answers

call a base-class explicit interface method in F#

Ok I derive a type B from a base class A. A implements IDisposable explicit but I have to do additional cleanup in B, so I implement IDisposable in B: interface IDisposable with member i.Dispose() = // ... additional work …
Random Dev
  • 51,810
  • 9
  • 92
  • 119
15
votes
3 answers

Why does calling an explicit interface implementation on a value type cause it to be boxed?

My question is somewhat related to this one: How does a generic constraint prevent boxing of a value type with an implicitly implemented interface?, but different because it shouldn't need a constraint to do this because it's not generic at all. I…
Random832
  • 37,415
  • 3
  • 44
  • 63
14
votes
2 answers

Is there a way to invoke explicitly implemented method/property via reflection in .NET?

I need to be able to determine if a given method or property comes from a particular interface and is explicitly implemented. Has anyone done this and is it actually possible to get this information by the means of .NET reflection? Update As can…
Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
14
votes
3 answers

Explicit implementation of IDisposable

Although there are quite a lot of Q&As regarding IDisposable to be found on SO, I haven't found an answer to this yet: I usually follow the practice that when one of my classes owns an IDisposable object then it also implements IDisposable and calls…
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
14
votes
2 answers

Why do explicit interface calls on generics always call the base implementation?

Why do explicit C# interface calls within a generic method that has an interface type constraint always call the base implementation? For example, consider the following code: public interface IBase { string Method(); } public interface…
Fanblade
  • 451
  • 2
  • 9
12
votes
2 answers

Why is HashSet.IsReadOnly explicit?

This var h = new HashSet(); var r = h.IsReadOnly; does not compile. I have to do var r = ((ICollection)h).IsReadOnly; why wasn't IsReadOnly implemented normally? (I'm not asking how, but why)
ripper234
  • 222,824
  • 274
  • 634
  • 905
11
votes
2 answers

Object initializer with explicit interface in C#

How can I use an object initializer with an explicit interface implementation in C#? public interface IType { string Property1 { get; set; } } public class Type1 : IType { string IType.Property1 { get; set; } } ... //doesn't work var v = new…
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
11
votes
2 answers

Fortran - explicit interface

I'm very new to Fortran, and for my research I need to get a monster of a model running, so I am learning as I am going along. So I'm sorry if I ask a "stupid" question. I'm trying to compile (Mac OSX, from the command line) and I've already…
Geraldine
  • 771
  • 3
  • 9
  • 23
1
2 3 4 5 6