Properties vs Methods

后端 未结 16 1671
傲寒
傲寒 2020-11-22 08:23

Quick question: When do you decide to use properties (in C#) and when do you decide to use methods?

We are busy having this debate and have found some areas where it

相关标签:
16条回答
  • 2020-11-22 08:51

    I come from java an i used get.. set.. method for a while.

    When i write code, i don't ask to my self: "accessing this data is simple or require a heavy process?" because things can change (today retrive this property is simple, tomonrow can require some or heavy process).

    Today i have a method SetAge(int age) tomonrow i will have also method SetAge(date birthdate) that calculate the age using the birthdate.

    I was very disappointed that the compiler transform property in get and set but don't consider my Get... and Set.. methods as the same.

    0 讨论(0)
  • 2020-11-22 08:52

    Properties are really nice because they are accessible in the visual designer of visual studio, provided they have access.

    They use be used were you are merely setting and getting and perhaps some validation that does not access a significant amount of code. Be careful because creating complex objects during validation is not simple.

    Anything else methods are the preferred way.

    It's not just about semantics. Using properties inappropriate start having weirdness occur in the visual studio visual designer.

    For instance I was getting a configuration value within a property of a class. The configuration class actually opens a file and runs an sql query to get the value of that configuration. This caused problems in my application where the configuration file would get opened and locked by visual studio itself rather than my application because was not only reading but writing the configuration value (via the setter method). To fix this I just had to change it to a method.

    0 讨论(0)
  • 2020-11-22 08:53

    Properties should only be simple set and get one liners. Anything more and it should really be moved to a method. Complex code should always be in methods.

    0 讨论(0)
  • 2020-11-22 08:54

    You need only look at the very name... "Property". What does it mean? The dictionary defines it in many ways, but in this case "an essential or distinctive attribute or quality of a thing" fits best.

    Think about the purpose of the action. Are you, in fact, altering or retrieving "an essential or distinctive attribute"? In your example, you are using a function to set a property of a textbox. That seems kind of silly, does it not?

    Properties really are functions. They all compile down to getXXX() and setXXX(). It just hides them in syntactic sugar, but it's sugar that provides a semantic meaning to the process.

    Think about properties like attributes. A car has many attributes. Color, MPG, Model, etc.. Not all properties are setable, some are calculatable.

    Meanwhile, a Method is an action. GetColor should be a property. GetFile() should be a function. Another rule of thumb is, if it doesn't change the state of the object, then it should be a function. For example, CalculatePiToNthDigit(n) should be a function, because it's not actually changing the state of the Math object it's attached to.

    This is maybe rambling a bit, but it really boils down to deciding what your objects are, and what they represent. If you can't figure out if it should be a property or function, maybe it doesn't matter which.

    0 讨论(0)
提交回复
热议问题