Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To use a read-only property or a method?

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.

like image 227
Dave New Avatar asked May 30 '13 06:05

Dave New


People also ask

What is a read only property?

Read only means that we can access the value of a property but we can't assign a value to it. When a property does not have a set accessor then it is a read only property. For example in the person class we have a Gender property that has only a get accessor and doesn't have a set accessor.

What is a read only property in Java?

Defining read-only class in Java If we make a class read-only, then we can't modify the properties or data members value of the class. If we make a class read-only, then we can only read the properties or data members value of the class.

What is read only property in C sharp?

In a field declaration, readonly indicates that assignment to the field can only occur as part of the declaration or in a constructor in the same class. A readonly field can be assigned and reassigned multiple times within the field declaration and constructor.

What is the difference between read-only and write-only properties?

A property without a set accessor is considered read-only. A property without a get accessor is considered write-only. A property that has both accessors is read-write. Unlike fields, properties are not classified as variables. Therefore, you cannot pass a property as a ref or out parameter.

How do you make a property read-only without a set accessor?

A property without a set accessor is considered read-only. A property without a get accessor is considered write-only. A property that has both accessors is read-write. In C# 9 and later, you can use an init accessor instead of a set accessor to make the property read-only.

What is a read-only-property in JavaScript?

A Read-Only-Property is a property decorator without a setter. The student id here is a read-only-property since it doesn’t have a setter. In general terms, it means that the value is not changeable. To understand let’s take one more example: To fix this one setter is to be added in this code.

How to declare a read-only auto-property?

As of 2015's C# 6 you can declare and initialise a read-only auto-property in one line: You can set the value from the constructor but not other methods. Show activity on this post. A property that has only a getter is said to be readonly.


2 Answers

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.

like image 125
Carsten Avatar answered Sep 21 '22 10:09

Carsten


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

like image 20
Micha Avatar answered Sep 20 '22 10:09

Micha