property in an interface

R

rodchar

hey all,
can and should a property be included in an interface class? if so, can
someone please show me a simple example?

thanks,
rodchar
 
J

Jon Skeet [C# MVP]

can and should a property be included in an interface class? if so, can
someone please show me a simple example?

Properties can indeed be part of an interface - IEnumerable.Current is
a good example.

So you'd have:

public interface ISomething
{
void SomeMethod();
string SomeReadonlyProperty { get; }
string SomeWritableProperty { get; set; }
}

Readonly properties in the interface can still be *implemented* with
writable properties.

Jon
 
I

Ignacio Machin ( .NET/ C# MVP )

hey all,
can and should a property be included in an interface class? if so, can
someone please show me a simple example?

thanks,
rodchar

Hi,

Of course they can and most of the time they have to.
how to declare it:
interface XXX{
int AnInt{ get;set;}

Take a look at MSDN lib, there you can find more details about it.
 
R

rodchar

hi all again,
i could reaally use a property in one of my classes but this property would
not be of any use in another one of my classes that also implement this
interface. is it ok to go ahead and add it?
if so, how would you handle this property in the class that has no use for it?

thanks,
rod.
 
A

Arne Vajhøj

rodchar said:
can and should a property be included in an interface class? if so, can
someone please show me a simple example?

Yes. It is one of the advantages of properties over public fields.

Arne
 
H

Hans Kesting

rodchar formulated the question :
hi all again,
i could reaally use a property in one of my classes but this property would
not be of any use in another one of my classes that also implement this
interface. is it ok to go ahead and add it?
if so, how would you handle this property in the class that has no use for
it?

thanks,
rod.

The interface specifies a "minimum" number of methods and properties,
so you can add any number of methods/properties that are not covered by
the interface. But this also means that when you use the class via the
interface, you can't use that property.

If you want to specify that extra property *as part of* the interface,
then *every* class that implements that interface should implement that
property. As far as the compiler is concerned, a "throw
NotImplementedException();" is a good implementation, only your code
will receive that exception when it uses the "wrong" class through that
interface.
But if the other objects can't provide a meaningful implementation,
then that property probably does not belong in the interface.

Hans Kesting
 
R

rodchar

how about events can they be part of an interface? i tried putting one in my
interface and then implementing in a class and got the following stub:


event EventHandler MyInterface.MyEvent
{
add { throw new Exception("The method or operation is not
implemented."); }
remove { throw new Exception("The method or operation is not
implemented."); }
}

what is the above all about? the add and remove?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top