<p>It strikes me that Properties in C# should be use when trying to manipulate a field in the class. But when there's complex calculations or database involved, we should use a getter/setter.</p>

<p>Is this correct?</p>

<p>When do you use s/getter over properties?</p>

[{'answer_id': 67076, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': '<p>If your language supports properties, just use properties.</p>\n'}, {'answer_id': 67085, 'author': 'Sam', 'author_id': 9406, 'author_profile': 'https://Stackoverflow.com/users/9406', 'pm_score': 1, 'selected': False, 'text': '<p>This is all personal preference. When it gets compiled it turns out to be getter/setter functions either way.</p>\n\n<p>I personally use properties when setting and retrieving member values without any side affects. If there are side affects to retrieving/saving the value, then I use a function.</p>\n'}, {'answer_id': 67105, 'author': 'Torlack', 'author_id': 5243, 'author_profile': 'https://Stackoverflow.com/users/5243', 'pm_score': 2, 'selected': False, 'text': "<p>Use the properties.  One interesting note from MS's framework design guidelines book is that if you have a property and need to add extra methods for more complex set/get, then you should eliminate the property and go with only get/set methods.</p>\n"}, {'answer_id': 67186, 'author': 'Sean Hanley', 'author_id': 7290, 'author_profile': 'https://Stackoverflow.com/users/7290', 'pm_score': 1, 'selected': False, 'text': "<p>I'd say always ask yourself which makes more sense.  Methods tend to be understood as actions to perform and are usually worded as such &mdash; <code>open()</code>, <code>flush()</code>, <code>parse()</code>.  Properties tend to be understood as fancier fields/variables &mdash; <code>DisplayName</code>, <code>AutoSize</code>, <code>DataSource</code>.</p>\n\n<p>This tends to come up a lot with custom control development I've noticed.  Since it has the potential of being used by many other people down the road who didn't write it and you might not be around to ask, best go with a design that makes logical sense and doesn't surprise your fellow developers.</p>\n"}, {'answer_id': 67215, 'author': 'Jeffrey L Whitledge', 'author_id': 10174, 'author_profile': 'https://Stackoverflow.com/users/10174', 'pm_score': 1, 'selected': False, 'text': '<p>I tend to use setters when a value is write-only or there are multiple values to be set at once (obviously). Also my instinct, like yours, is to use getters and setters as a signal that a process may be long-running, spawn threads, or do some other non-trivial work. Also, if a setter has non-obvious prerequisites in the class, I might use a getter or setter instead, since people rarely read documentation on properties, and properties are expected to be accessable at all times. But even in these circumstances I might use a property if it will potentially make the calling code read better.</p>\n'}, {'answer_id': 67217, 'author': 'JRoppert', 'author_id': 6777, 'author_profile': 'https://Stackoverflow.com/users/6777', 'pm_score': 0, 'selected': False, 'text': '<p>Forget the Getter and Setter methods. Just use Properties. </p>\n\n<p>An interesting thing to mention is that Properties are ending up as Setter and/or Getter method(s) in the assembly. Setter and/or Getter become a Property just by a little bit of metadata. So in fact, properties = setter/getter methods.</p>\n'}, {'answer_id': 67229, 'author': 'OwenP', 'author_id': 2547, 'author_profile': 'https://Stackoverflow.com/users/2547', 'pm_score': 5, 'selected': True, 'text': '<p>The .NET design guidelines provide some answers to this question in the <a href="http://msdn.microsoft.com/en-us/library/bzwdh01d(VS.71).aspx#cpconpropertyusageguidelinesanchor1" rel="noreferrer">Properties vs. Methods</a> section.</p>\n\n<p>Basically, properties have the same semantics as a field.  You shouldn\'t let a property throw exceptions, properties shouldn\'t have side effects, order shouldn\'t matter,  and properties should return relatively quickly.  If any of those things could happen, it\'s better to use a method.  The guidelines also recommend using methods for returning arrays.</p>\n\n<p>When deciding whether to use a property or method, it helps if I think of it like a field.    I think about the behavior of the property and ask myself, "If this were a field on the class, would I be surprised if it behaved the way it does?"  Consider, for example, the <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.getstream.aspx" rel="noreferrer">TcpClient.GetStream method</a>.  It can throw several exceptions based on if the connection is made, and it\'s important that the TcpClient is configured before you try to get the stream.  Because of this, it is a Get method rather than a property.</p>\n\n<p>If you take a good look at the design guidelines, you\'ll see that it\'s usually <em>not</em> a matter of preference; there\'s good reasons to use methods instead of properties in certain cases.</p>\n'}, {'answer_id': 67276, 'author': 'Thomas Eyde', 'author_id': 3282, 'author_profile': 'https://Stackoverflow.com/users/3282', 'pm_score': 0, 'selected': False, 'text': '<p>Properties should be fast, as they have a certain promise of just being there. They are also mandatory for databinding.</p>\n\n<p>And they should have <strong>no side-effects</strong>.</p>\n'}, {'answer_id': 4258086, 'author': 'supercat', 'author_id': 363751, 'author_profile': 'https://Stackoverflow.com/users/363751', 'pm_score': 0, 'selected': False, 'text': '<p>Microsoft\'s answer is good, but I\'d add a few more rules for read-write properties (which Microsoft violates sometimes, btw, causing much confusion): (1) A property setter should generally not affect the observable properties of objects which are not considered to be part of the object whose property is being set; (2) Setting a property to one value and then another should leave any affected objects in the same (observable) state as simply setting it to the second value; (3) Setting a property to the value returned by its getter should have no observable effect; (4) Generally, setting a property should not cause any other read-write properties to change, though it may change other read-only properties (note that most violations of this rule would violate #2 and/or #3, but even when those rules would not be violated such designs still seem dubious).  Making an object usable in the designer may require giving it some properties which don\'t follow these rules, but run-time changes which will not follow such semantics should be done by setter methods.</p>\n\n<p>In many cases, it may be appropriate to have a read-only property and a separate "Set" method (that would be my preference, for example, for a control\'s "Parent" property).  In other cases, it may be useful to have several related ReadOnly properties that are affected by one read/write property (e.g. it may be useful to have a read-only property that indicates whether a control and all its parents are visible, but such functionality should not be included in a read-write Visible property).</p>\n'}]
['https://Stackoverflow.com/questions/67063', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10088/']