Interface Question

  • Thread starter Thread starter psandler70
  • Start date Start date
P

psandler70

All,

This might be a stupid question.

When defining an interface, is it possible to force the class that
implements it to support a property/method with the same name, but not
force a specific type?

So the interface would specify:

object myValue
{
get;
set;
}

and the implementing class would have:

public int myValue
{
get {return _myValue;}
set { _myValue = value;}
}

The above is a simple example. What I'm really trying to do is have an
interface with a List<object>, and implementing classes that use
List<myTypeA>, List<myTypeB>, etc.

Would this defeat the purpose of having an interface to begin with?


Thanks for any insight,

Phil
 
This might be a stupid question.

When defining an interface, is it possible to force the class that
implements it to support a property/method with the same name, but not
force a specific type?

No, I'm afraid not - covariant return types aren't supported. Read on,
however...
Would this defeat the purpose of having an interface to begin with?

Sort of. Often it's useful to have an interface which specifies a base
type to be returned, but have the implementation specify a more
specific type for clients which know which implementation they're
dealing with. In that case, explicit interface implementation lets you
effectively define the method twice - once for the interface
implementation and once "normally".
 
When defining an interface, is it possible to force the class that
implements it to support a property/method with the same name, but not
force a specific type?

Unless I'm misunderstanding your question, this is what open
interfaces do. For example, given

interface IOpen<T>
{
T MyValue { get; set; }
List<T> MyList { get; set; }
}

an actual class might support IOpen<int> like

class Closed: IOpen<int> {}

class Open<T>: IOpen<T> {}

--

Delphi skills make .NET easy to learn

..NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>

In print, in stores.
 
Back
Top