Tuesday, 15 February 2011

c# - To use a read-only property or a method? -


I am " mapped " to unearth the state of an event in a class The requirement is that the result is determined by the original check, it is not to highlight the value of only one area, I am unsure about whether I should use a readable asset or method or not.

Read-only property:

  Public Bull ISM {{return mapfield! = Zero; }}  

Method:

  Public Butt ISMF () {Back Mapfeed! = Null; }  

I have read MSDN, but I am still unsure.

C # standard says

§8.7.4

A property is a member who provides access to the attributes of an object or class. Examples of properties include string length, font size, caption of a window, a customer's name, and so on. Properties is a natural extension of the field. Both are connected types, and the syntax is similar to access fields and properties. However, unlike fields, properties do not denote collection locations. Instead, there are entities in properties that specify the details to be executed when their values ​​are read or written.

While the methods are defined

§8.7 3

A Method is a member who executes a calculation or verb which can be done by an object or class. Methods have a (probable empty) list of formal criteria, a return value (as long as the return-type of the method is zero), and is either static or non-static

Explaining the properties, the logic contains methods in ways that are used to realize the use of properties and methods . And this is the reason that if you are highlighting the data, then you should prefer only the readable property. There is no logic in your case that modifies the internal state of your object. You want to grant access to an object's attribute .

Whether an example of your object is Ismapped , is a feature of your object that contains an investigation, but this is the reason that you have properties to access it . The attribute can be defined using logic, but the argument should not be revealed. Like the example mentioned in the first quote: String.Length Imagine the property Depending on the implementation, it may be that this property has a rigidity through the string and calculates the characters. It also performs an operation, but it gives a statement on the characteristics of the internal condition / object "from outside".


No comments:

Post a Comment