Home>

What are the criteria for using properties and methods?
I understand that properties are states and methods are behaviors.
For example, suppose you have a value object that represents a person's name.

public sealed class PersonName: ValueObject
{
    private string _firstName;
    private string _lastName;
    public PersonName (string firstName, string lastName)
    {
        _firstName = firstName;
        _lastName = lastName;
    }
    ...
}

When adding a value for screen display to this object

public string DisplayValue =>$"{_firstName} {_lastName}";

Should it be a property like

public string ToDisplayValue () =>$"{_firstName} {_lastName}";

I'm not sure if it should be a method like this.
When debugging, properties are more convenient because you can see the values ​​immediately, but I think that the IDE will be overloaded (slowed down) by that amount.
The above is a simple case, so it may not change in either case, but when it becomes more complicated, what criteria should be used properly?

c#
  • Answer # 1

    The above is a simple case, so it may not change in either case, but when it becomes more complicated, what criteria should be used properly?

    I think that using a method instead of a property is a remnant of C/C ++ where the property couldn't be used (the programmer has it in his head, or there are organizational rules).

    In order to realize one of the object-oriented concepts "encapsulation", direct access to each field in the class is usually prohibited, and the value of each field is acquired or set by public property from the outside. The original purpose of the property is to do.

    In the C/C ++ era when properties weren't available, it seems that they had no choice but to use methods. See the article below.

    Set/Get and properties
    https://ufcpp.net/study/miscprog/accessor.html

    Another purpose of using properties is to ensure that the fields are used correctly according to the rules intended by the developer. For example, as in the following article, when a user sets a property in Visual Studio, it can throw an exception if it is out of range.

    Web Custom Control Exception Handling
    http://surferonwww.info/BlogEngine/post/2010/08/06/Exception-handling-by-web-custom-control.aspx

    You may also need to use properties. For example, when defining a model in Entity Framework Code First, or when using a data binding expression in an ASP.NET Web Forms app.

  • Answer # 2

    Well, honestly, it is written in detail by those who read the official reference.
    Properties (C # Programming Guide)

    Properties are members that provide a flexible mechanism for reading, writing, or calculating the value of private fields. Properties can be used like members of public data, but they are actually a special method called accessors. You can easily access your data while increasing the security and flexibility of your method.

    Properties are also methods, a way to get class members to access them with a brief description.
    Other languages ​​do not have a function equivalent to properties, so they are implemented with getter/setter methods.
    In the example presented, I don't think there is any particular problem with the property.
    If you need complicated parameters for acquisition and setting, you may have to use a normal method.

  • Answer # 3

    Proper use of properties and methods

    Suppose you define a method.
    That methodGet,SetIf the name starts with, the beginningGetOrSetShould be removed and defined as a property.


    ToDisplayValueIn the case of the method written asGetDisplayValueHaru853 says that it can be read as or should be read asthinkIf so, it should be a property.

    "No, this should be a method because it doesn't start with Get (ie it's not a getter)," says haru853.thinkIf so, it should be a method.

    Naming standards vary depending on people's senses and consciousness, company coding rules and culture.
    SoWhat do you think you should nameIs the standard for proper use.

    I,ToDisplayValueIsGetFullNameShould be namedthinkSo it should be named as followsthink..

    public string FullName =>$"{_firstName} {_lastName}";

  • Answer # 4

    What are the criteria for proper use?

    Properties if they are semantically accessors (getter/setter), methods otherwise

    Is it like that?

  • Answer # 5

    In the case of the example presented, it can be either a property or a method.
    I think that the judgment criteria will change from person to person.
    In the case of the example presented, if you think on your own basis
    I think you will judge what kind of changes are expected in the future.
    Since it is the name of the example, I imagine that there will be no change that adds an argument,
    I think I will use properties.

    However, in fact, I added an argument later,
    If you expect to add another feature,
    I think I will use the method.