N
N
Noortvel2016-01-01 20:31:28
Programming
Noortvel, 2016-01-01 20:31:28

Which is better to use for get set propeties or functions?

Which is better to use for get set propeties or functions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MrDywar Pichugin, 2017-01-02
@Noortvel

There is an object.
An object has:
* Fields
 * Properties
 * Methods
 * Constructors
 * Events
 * Nested types
**Fields**
A field describes and contains part of the type's state. Fields can be of any type supported by the runtime. Fields are most often private or protected, so they are only accessible from within the class itself or from classes derived from it. If a field's value can be changed from outside its type, then a property set accessor is typically used to do so.
**Properties**
A property specifies a particular value or state of a type, and defines methods for getting and setting the property's value. Properties can be simple types, collections of simple types, user-defined types, or collections of user-defined types. Properties are often used to ensure that the generic interface of a type is independent of the actual implementation of that type. This allows properties to represent values ​​that are not stored directly in the class (for example, when a property returns a computed value), or to perform validation before assigning values ​​to private fields.
**Methods**
A method describes the operations available on a particular type. The signature of a method specifies the valid types of all its parameters and return value.
In general, methods represent actions and properties represent data. Properties are meant to be used as fields; this means that properties should not be difficult to evaluate or lead to side effects. When it doesn't violate the guidelines below, you should use a property rather than a method, as less experienced developers find properties easier to use.
In the following situations, use a method rather than a property.
* Using an operation is many times slower than using a method to set a field. Even if you are going to provide an asynchronous version of the operation to avoid blocking the thread, using the operation is probably too expensive to be a property. In particular, operations that access the network or file system (as opposed to one-time access for initialization) should be designed with methods, not properties.
* The operation is a conversion, such as the Object.ToString method.
Each time the operation is called, it returns a new result, even if the parameters do not change. For example, each time the NewGuid method is called, it returns a new value.
* The operation has a significant and observable side effect. Note that filling up the internal cache is generally not considered an observable side effect.
* The operation returns a copy of the internal state (does not include copies of value type objects returned on the stack).
* The operation returns an array.
Property access is as fast as field access if get;set; have a simple implementation and the compiler did an inline optimization in which the method call was replaced by its body.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question