To use a read-only property or a method?

2019-02-01 20:17发布

I need to expose the "is mapped?" state of an instance of a class. The outcome is determined by a basic check. It is not simply exposing the value of a field. I am unsure as to whether I should use a read-only property or a method.

Read-only property:

public bool IsMapped
{
    get
    {
        return MappedField != null;
    }
}

Method:

public bool IsMapped()
{
    return MappedField != null;
}

I have read MSDN's Choosing Between Properties and Methods but I am still unsure.

12条回答
女痞
2楼-- · 2019-02-01 20:30

I would expect property as it only is returning the detail of a field. On the other hand I would expect

MappedFields[] mf;
public bool IsMapped()
{
     mf.All(x => x != null);
}
查看更多
放荡不羁爱自由
3楼-- · 2019-02-01 20:34

IMHO , the first read-only property is correct because IsMapped as a Attribute of your object, and you're not performing an action (only an evaluation), but at the end of the day consistancy with your existing codebase probably counts for more than semantics.... unless this is a uni assignment

查看更多
戒情不戒烟
4楼-- · 2019-02-01 20:35

I'd go for a property. Mostly because the first senctence on the referenced MSDN-article:

In general, methods represent actions and properties represent data.

查看更多
Viruses.
5楼-- · 2019-02-01 20:36

you should use the property because c# has properties for this reason

查看更多
Juvenile、少年°
6楼-- · 2019-02-01 20:37

The C# standard says

§ 8.7.4

A property is a member that provides access to a characteristic of an object or a class. Examples of properties include the length of a string, the size of a font, the caption of a window, the name of a customer, and so on. Properties are a natural extension of fields. Both are named members with associated types, and the syntax for accessing fields and properties is the same. However, unlike fields, properties do not denote storage locations. Instead, properties have accessors that specify the statements to be executed when their values are read or written.

while as methods are defined as

§ 8.7.3

A method is a member that implements a computation or action that can be performed by an object or class. Methods have a (possibly empty) list of formal parameters, a return value (unless the method’s return-type is void ), and are either static or non-static.

Properties and methods are used to realize encapsulation. Properties encapsulate data, methods encapsulate logic. And this is why you should prefer a read-only property if you are exposing data. In your case there is no logic that modifies the internal state of your object. You want to provide access to a characteristic of an object.

Whether an instance of your object IsMapped or not is a characteristic of your object. It contains a check, but that's why you have properties to access it. Properties can be defined using logic, but they should not expose logic. Just like the example mentioned in the first quote: Imagine the String.Length property. Depending on the implementation, it may be that this property loops through the string and counts the characters. It also does perform an operation, but "from the outside" it just give's an statement over the internal state/characteristics of the object.

查看更多
乱世女痞
7楼-- · 2019-02-01 20:38

I would use the property, because there is no real "doing" (action), no side effects and it's not too complex.

查看更多
登录 后发表回答