How to determine whether a property or a method is more appropriate(dotnettipoftheday.org)

submitted by manovichmanovich(755) 4 years, 3 months ago

Guidelines for choosing between implementing a class member as a property or a method.

6 comments |category: |Views: 2

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by DamienGDamienG(1405) 4 years, 3 months ago 0

"Calling the member twice in succession produces different results."

Not necessarily, DateTime.Now and Random.Next are examples of a valid propertys that produce different results when called in succession.

[)amien

Reply

posted by bokiboki(55) 4 years, 3 months ago 0

Minor correction: System.Random.Next is not a property.

Discussing DateTime.Now is a rather moot, academic exercise as it follows "Use a property when the member is a logical data member" [1] ;)

-- bg

[1] http://msdn2.microsoft.com/en-us/library/bzwdh01d(VS.71).aspx#cpconpropertyusageguidelinesanchor1

Reply

posted by DamienGDamienG(1405) 4 years, 3 months ago 0

Yeah sorry Random.Next isn't... but Stopwatch.Elapsed is ;-)

Perhaps it should be worded "Calling the member twice in succession produces different results with the exception of time-based activities"

[)amien

Reply

posted by yesthatmcgurkyesthatmcgurk(4063) 4 years, 3 months ago 0

Biggest one that is missed: The operation may throw an exception. You should never have to wrap property accesses within try/catch blocks.

Biggest one that's complete bullshit: If it returns an array. Why? Because you cannot guarantee callers won't change the state of the array.

1) How is this different than any property that exposes an object via a getter?
2) Have you seen the framework? Collections are regularly exposed via properties.

This "hint" is full of fail and ugly. NO KICK FOR U.

Reply

posted by powerrushpowerrush(3873) 4 years, 3 months ago 0

Yeah, I must agree that the arrays one I just ignored thinking Collections get returned in the framework. :)

On the bonus side, there is the link in the article to MS's Design Guidelines for Class Library Developers.

Reply

posted by SirkoSirko(105) 4 years, 3 months ago 0

Common sense should be used when applying any guidelines. These are guidelines not strict rules.

Regarding returning array: IMHO if by desing it is acceptable that array/collection will be modified by external code, then I think member can be implemented as property (every rule has an exception).

Reply

information Login or create an account to comment on this story