Would you use a protected field and property?

G

GG

I have an abstract class that looks like this

protected DateTime startDate = DateTime.Now;
protected DateTime endDate = DateTime.Now;
classConstructor(DateTime startDate,DateTime endDate)
{
this.startDate = startDate;
this.endDate = endDate;
}

The goal is when I inherit from this class to have access to startDate
and endDate.
I do not need to validate anything. Just use the protected fields as a
parameters. Also the dates will be used on other private methods in the
class.

Would I gain anything if I will add a property on the protected date
fields?


Thanks
 
D

Daniel O'Connell [C# MVP]

Other than plain cleanliness, properties won't offer you much.

Personally, I prefer to keep all fields private(except for tiny, internal,
data-only structures and constants, for which I use public fields). Using
properties allows you to pretty much tell derived classes what their
permitted access is. A readonly property won't allow the derived to assign
startDate, for example.

Also, if the class is being exposed publically, it is often advisable to
expose properties simply to maintain a consistent front. Its the method the
framework uses and the most common one among component library designers.



<GG> wrote in message I have an abstract class that looks like this

protected DateTime startDate = DateTime.Now;
protected DateTime endDate = DateTime.Now;
classConstructor(DateTime startDate,DateTime endDate)
{
this.startDate = startDate;
this.endDate = endDate;
}

The goal is when I inherit from this class to have access to startDate
and endDate.
I do not need to validate anything. Just use the protected fields as a
parameters. Also the dates will be used on other private methods in the
class.

Would I gain anything if I will add a property on the protected date
fields?


Thanks
 

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