469,581 Members | 1,984 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,581 developers. It's quick & easy.

getter/setter with no body

Hi Folks

Can anyone confirm that the code

public string Name
{
get;
set;
}

is used to define an abstract property accessor?

would I also need to use the abstract keyword eg

public abstract string Name
{
get;
set;
}

Many Thanks

Kron

Aug 19 '06 #1
5 5977
Kron,

Have you tried it and compiled it?

public string Name {get; set;}

The above will not work. The compiler expects an implementation to the
property.

public abstract string Name {get; set;}

The above will work. It will require derived classes to override the
implementation.

If you want to provide a default implementation which can be overriden,
then you can use virtual:

public virtual string Name
{
get
{
return name;
}
set
{
name = value;
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<kr****@yahoo.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
Hi Folks

Can anyone confirm that the code

public string Name
{
get;
set;
}

is used to define an abstract property accessor?

would I also need to use the abstract keyword eg

public abstract string Name
{
get;
set;
}

Many Thanks

Kron

Aug 19 '06 #2
Thanks Nicholas, your answer is most helpful :)

I played around with this after I posted and found the results to be
exactly as you say. Next time I'll try it out first ;)

Many Thanks

Kron
Nicholas Paldino [.NET/C# MVP] wrote:
Kron,

Have you tried it and compiled it?

public string Name {get; set;}

The above will not work. The compiler expects an implementation to the
property.

public abstract string Name {get; set;}

The above will work. It will require derived classes to override the
implementation.

If you want to provide a default implementation which can be overriden,
then you can use virtual:

public virtual string Name
{
get
{
return name;
}
set
{
name = value;
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<kr****@yahoo.comwrote in message
news:11**********************@74g2000cwt.googlegro ups.com...
Hi Folks

Can anyone confirm that the code

public string Name
{
get;
set;
}

is used to define an abstract property accessor?

would I also need to use the abstract keyword eg

public abstract string Name
{
get;
set;
}

Many Thanks

Kron
Aug 19 '06 #3
On Sat, 19 Aug 2006 12:57:39 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comwrote:
>Kron,

Have you tried it and compiled it?

public string Name {get; set;}

The above will not work. The compiler expects an implementation to the
property.
Yep, but if you drop the "public" it will work in an interface, it's
the syntax for specifying a property member. (I am sure you knew this
but maybe not the O.P...)
--
Philip Daniels
Aug 19 '06 #4
Philip,

If you drop off the public, then it is assumed to be private. You still
get a compiler error. It doesn't assume abstract when there is no public
access modifier.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Philip Daniels" <fo*@foo.comwrote in message
news:cp********************************@4ax.com...
On Sat, 19 Aug 2006 12:57:39 -0400, "Nicholas Paldino [.NET/C# MVP]"
<mv*@spam.guard.caspershouse.comwrote:
>>Kron,

Have you tried it and compiled it?

public string Name {get; set;}

The above will not work. The compiler expects an implementation to
the
property.

Yep, but if you drop the "public" it will work in an interface, it's
the syntax for specifying a property member. (I am sure you knew this
but maybe not the O.P...)
--
Philip Daniels

Aug 21 '06 #5
Nicholas Paldino [.NET/C# MVP] wrote:
Philip,

If you drop off the public, then it is assumed to be private. You
still get a compiler error. It doesn't assume abstract when there is
no public access modifier.
.... unless it's in an interface definition, as the previous poster said.

-cd
Aug 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kiwi | last post: by
12 posts views Thread by Adam Sandler | last post: by
3 posts views Thread by Martin Pöpping | last post: by
2 posts views Thread by Amie | last post: by
2 posts views Thread by Josh Valino | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.