getter/setter with no body

  • Thread starter Thread starter kronrn
  • Start date Start date
K

kronrn

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
 
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.
 
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

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]
- (e-mail address removed)

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
 
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,

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 said:
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
 

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

Back
Top