is partial property implementation possible?

B

Ben Voigt

The C# Language Specification says:

A virtual property declaration specifies that the accessors of the property
are virtual. The virtual modifier applies to both accessors of a read-write
property - it is not possible for only one accessor of a read-write property
to be virtual.

An abstract property declaration specifies that the accessors of the
property are virtual, but does not provide an actual implementation of the
accessors. Instead, non-abstract derived classes are required to provide
their own implementation for the accessors by overriding the property.
Because an accessor for an abstract property declaration provides no actual
implementation, its accessor-body simply consists of a semicolon.





Does this mean it is possible for only one accessor of a read-write property
to be abstract (both get and set are virtual)?

I tried:

interface A
{
int X { get; set; }
}
abstract class B : A
{
virtual public int X
{
get
{
return 0;
}
}
}
class C : B
{
override public int X
{
// use base class getter
set
{
throw new Exception("Illegally tried to set X.");
}
}
}



But no matter omit the setter in abstract class B, leave it empty, try to
use the abstract keyword, I always get an error. I know I can use an
abstract member function for this, but how can I leave the setter undefined
(since B is abstract after all)?
 
W

Willy Denoyette [MVP]

| The C# Language Specification says:
|
| A virtual property declaration specifies that the accessors of the
property
| are virtual. The virtual modifier applies to both accessors of a
read-write
| property - it is not possible for only one accessor of a read-write
property
| to be virtual.
|
| An abstract property declaration specifies that the accessors of the
| property are virtual, but does not provide an actual implementation of the
| accessors. Instead, non-abstract derived classes are required to provide
| their own implementation for the accessors by overriding the property.
| Because an accessor for an abstract property declaration provides no
actual
| implementation, its accessor-body simply consists of a semicolon.
|
|
|
|
|
| Does this mean it is possible for only one accessor of a read-write
property
| to be abstract (both get and set are virtual)?
|
| I tried:
|
| interface A
| {
| int X { get; set; }
| }
| abstract class B : A
| {
| virtual public int X
| {
| get
| {
| return 0;
| }
| }
| }
| class C : B
| {
| override public int X
| {
| // use base class getter
| set
| {
| throw new Exception("Illegally tried to set X.");
| }
| }
| }
|
|
|
| But no matter omit the setter in abstract class B, leave it empty, try to
| use the abstract keyword, I always get an error. I know I can use an
| abstract member function for this, but how can I leave the setter
undefined
| (since B is abstract after all)?
|
|

If you mean:
set{}
by " leave it empty"?

this should work.


Willy.
 
B

Bruce Wood

It appears that the compiler will not allow you to implement "half" of
an interface property. You must either declare it "abstract," in which
case you can define neither the getter nor the setter:

public abstract int X { get; set; }

or you must declare it "virtual," in which case it can have no abstract
component and you must define both the getter and the setter:

public virtual int X
{
get { ... }
set { ... }
}

There appears to be no way to implement the getter but require that
derived classes implement the setter.
 

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