Conversions in getters?

If I could get some opinions on this I'd really appreciate it.

So say I have a distance class that looks something like this:

public class Distance { // let's ignore how this gets set for now... private decimal meters; // Option 1 public decimal AsMiles => meters * 0.000621371m; // Option 2 public decimal AsMiles() { return meters * 0.000621371m; } } 

Is it preferable to implement AsMiles as a property, like option 1, or as a function, like option 2?

I know that "get" properties shouldn't really do work, but is this conversion really that big of a deal? I think I may have also heard that "get" properties doing work is a bad deal for GUIs as well, but this probably isn't something that'd be directly displayed in a GUI.

I'm pretty sure option 2 is better, so maybe I'm just property happy (it's been a while since I've had the pleasure of using C#), but to me properties make the underlying data feel more ambiguous, which I quite like.

by jontheburger via /r/csharp

Leave a Reply