Method or property?

  • Thread starter Thread starter Simon Harvey
  • Start date Start date
S

Simon Harvey

Hi,

I have a need to create a method/property called isInRole().

My question is whether this should be implemented as a method or a property?
Is there any particular benefit to be had from doing it one way over the
other?

Thanks for any advice you can share

Simon
 
Hi Simon,
I have a need to create a method/property called isInRole().

My question is whether this should be implemented as a method or a property?
Is there any particular benefit to be had from doing it one way over the
other?

Good practise is to choose a mathod/property:
1. Property - if value could be fast accessed (e.g. private variable)
2. Method - if value should be calculated or its access take a time
(e.g. searching a collection to find a value)

Regards

Marcin
 
Hi,

I think it's a semantic problem. Properties refer to "states" while mehods
refers to "actions". Method's name must be verbs (Run(), Load(), etc) while
property's name must be nouns or questions (State, IsPostback).

Sometimes a property needs an argument (double.IsInfinity(n);) then we must
use a method, because a property does not accept arguments.

Under ground a property becames in one or two methods: one for get accessor,
one for set accessor.

Conclussion: user what you want. :)

Regards,
LC
 
Back
Top