Re: Typed List



I'd just recommend - reverse the implementation so that the explicit interface method calls the public method. In this special case you'll avoid boxing/unboxing, but IMHO, the public implementation should be primary and the explicit interface implementation should only adapt the interface method signature to the actual implementation.

interface IMyInterface {
  void Function(object o);
}

class MyClass : IMyInterface {
  // explicit implementation (hidden)
  void IMyInterface.Function(object o) {
    // call the public typed implementation
    Function((int) o);
  }

  // own, typed implementation
  public void Function(int i) {
    // ... do something with i
  }
}

Just my 2c
Stefan

Markus Stoeger wrote:
MuZZy wrote:

And the same question about IEnumerator implementation - what should i do so that "Current" would return a typed object of type "MyType", not of type 'object'?


Lookup "explicit interface implementation". It allows you to "hide" the untyped interface members and implement typed ones instead. Thats what the collection classes do too.

interface IMyInterface {
  void Function(object o);
}

class MyClass : IMyInterface {
  // explicit implementation (hidden)
  void IMyInterface.Function(object o) {
    // ... do something with o
  }

  // own, typed implementation
  public void Function(int o) {
     // call the exlicitely implemented function
    ((IMyInterface)this).Function(o);
  }
}

Now if you have an instance of MyClass you'll only see the typed function "Function" with the int parameter. The untyped function is hidden (.. or only accessible when you cast the object to IMyInterface).

hth,
Max
.



Relevant Pages

  • Trouble with C++ class Inheriting from a C# parameterized class with explicit interface implementati
    ... I have implemented, in C#, a generic collection which has explicit interface ... If I try to inherit from this in C++ I get errors telling me that I am ... an implementation for the interface method 'void ...
    (microsoft.public.dotnet.languages.vc)
  • Re: What does the modifier have methid from interface?
    ... hmm i've said in the interface not in the implementing it class... ... >>in the interface method is public virtual ... > class Base: I ... > public void F ...
    (microsoft.public.dotnet.languages.csharp)
  • Problem getting events from C# to VJ++
    ... interface IWeatherEvents: IUnknown ... public delegate void WeatherChangedDelegate(int nTemperature); ... // float Temperature ... private float m_fTemperature = 0; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Question about Java updates installed?
    ... public long add(int a, int b) ... either encapsulate the add method in an interface and then ... void test() throws Exception { ... A multicast delegate was also possible. ...
    (comp.lang.java.advocacy)
  • Re: com interop
    ... with showing a movie, and yet, you want the spell checker from word? ... > i need to know what interface ms-word's spell checker uses. ... > void Pause(); ... > // DisplayUsage(); ...
    (microsoft.public.dotnet.languages.csharp)