Properties vs Methods

后端 未结 16 1669
傲寒
傲寒 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:27

    Properties are a way to inject or retrieve data from an object. They create an abstraction over variables or data within a class. They are analogous to getters and setters in Java.

    Methods encapsulate an operation.

    In general I use properties to expose single bits of data, or small calculations on a class, like sales tax. Which is derived from the number of items and their cost in a shopping cart.

    I use methods when I create an operation, like retrieving data from the database. Any operation that has moving parts, is a candidate for a method.

    In your code example I would wrap it in a property if I need to access it outside it's containing class:

    public Label Title 
    {
       get{ return titleLabel;}
       set{ titleLabel = value;}
    }
    

    Setting the text:

    Title.Text = "Properties vs Methods";
    

    If I was only setting the Text property of the Label this is how I would do it:

    public string Title 
    {
       get{ return titleLabel.Text;}
       set{ titleLabel.Text = value;}
    }
    

    Setting the text:

    Title = "Properties vs Methods";
    
    0 讨论(0)
  • 2020-11-22 08:27

    If you're setting an actual property of your object then you use a property.

    If you're performing a task / functionality then you use a method.

    In your example, it is a definite property being set.

    If however, your functionality was to AppendToLabel then you would use a method.

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

    As a matter of design Properties represent Data or Attributes of class object, While methods are actions or behaviors of class object.

    In .Net, world there are other implications of using Properties:

    • Properties are used in Databinding, while get_ / set_ methods are not.
    • XML serialization user properties as natural mechanism of serilization.
    • Properties are accessed by PropertyGrid control and intern ICustomTypeDescriptor, which can be used effectively if you are writing a custom library.
    • Properties are controlled by Attributes, one can use it wisely to design Aspect Oriented softwares.

    Misconceptions (IMHO) about Properties' usage:

    • Used to expose small calculations: ControlDesigner.SelectionRules's get block runs into 72 lines!!
    • Used to expose internal Data structures: Even if a property does not map to an internal data member, one can use it as property, if its an attribute of your class. Viceversa, even if its an attribute of your class properties are not advisable, to return array like data members (instead methods are used to return deep copy of members.)

    In the example here it could have been written, with more business meaning as:

    public String Title
    {
        set { Label.Text = text; }
    }
    
    0 讨论(0)
  • 2020-11-22 08:31

    Searching through MSDN, I found a reference on Properties vs Methods that provides some great guidelines for creating methods:

    • The operation is a conversion, such as Object.ToString.
    • The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
    • Obtaining a property value using the get accessor would have an observable side effect.
    • Calling the member twice in succession produces different results.
    • The order of execution is important. Note that a type's properties should be able to be set and retrieved in any order.
    • The member is static but returns a value that can be changed.
    • The member returns an array. Properties that return arrays can be very misleading. Usually it is necessary to return a copy of the internal array so that the user cannot change internal state. This, coupled with the fact that a user can easily assume it is an indexed property, leads to inefficient code.
    0 讨论(0)
  • 2020-11-22 08:36

    This is simple.

    1: use property when you want your data should be validated before storing in field. So in this way property provides encapsulation for your fields. Because if you leave your fields public end user may assign any value which may or may not be valid as per your business requirement like age should be greater than 18. So before value is store corresponding field we need to check its validity. In this way properties represent data.

    2: Use method when you want perform some action like you are supplying some data as parameter and your method is doing some processing on the basis of supplied values and returning processed value as output. Or you want to change value of some field by this calculation. "In this way method represents action".

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

    Symantically properties are attributes of your objects. Methods are behaviors of your object.

    Label is an attribute and it makes more sense to make it a property.

    In terms of Object Oriented Programming you should have a clear understanding of what is part of behavior and what is merely an attribute.

    Car { Color, Model, Brand }

    A car has Color, Model and Brand attributes therefore it does not make sense to have a method SetColor or SetModel because symantically we do not ask Car to set its own color.

    So if you map the property/method case to the real life object or look at it from symantic view point, your confusion will really go away.

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