The difference between a property and a field

Source: Internet
Author: User

From the declaration of both, the public field is simply a simple public variable that the class exposes with the public modifier, and the property is the encapsulation of the field, which uses get and set accessors to control how the field values are set or returned.

Because the nature of the property is a method (get or Set method), there is no concept of attributes in IL. So for the assignment and value operations that are commonly used in the development process, using public variables will certainly be faster than using attributes, and slightly higher in performance (methods and variables which speed needless to say).

Although the public field is fast in speed, it must be open in the fields, so that the caller of the object can directly modify its value, whether the content of the value is legitimate, whether there is an error in the run, there is no guarantee, and thus greatly reduces the reusability of the class, instead, the property is similar to the method. It can handle the value of the stored variable, and if it is not legal, it can be transformed in place or warned directly. This is a great benefit for the use of objects of this class, and during the run, problems caused by errors in the values of the public variables are greatly reduced.

From the above content, both have advantages and disadvantages, in the actual project development process, we choose which way to use it?

If the following conditions are met, then we can boldly use public fields:

1. Allow free reading and writing;

2. The range of values is constrained only by the data type, without any other specific restrictions;

3. The change of value does not require the corresponding change of any other member in the class;

The use of a property is exactly the opposite of a variable, and a property should be used as long as any of the following conditions are true:

1. Required fields are read-only or write-only;

2. You need to limit the range of values for the field;

3. When changing the value of a field, you want to change some other state of the object;

Summary: Although public fields and properties can be used under appropriate conditions during the development of the actual project, we should use the property rather than the data member (field) as much as possible, and set all the fields as private fields, and if you want to expose them, encapsulate them as attributes. This is also the way Microsoft recommends.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------

In C #, we have a very free, unrestricted access to public fields, but in some cases we may want to limit the values that can only be assigned to a field, or to require fields to be read-only or write-only, or to change some other state of an object when changing a field, which cannot be done by a single field. Then there is the attribute, which contains two blocks: Set and Get,set block are responsible for the writing of the property, the get block is responsible for the property read work. There are a few other things you can do in two blocks, such as verifying that the assigned value meets the requirements in set and deciding whether to assign a value. When one of the pieces is missing, the property is only readable or writable, and the properties in the set and get blocks must have one, because a property that cannot be read and cannot be written is meaningless.

Class MyClass

{

Private string name;

public string Name

{

get {return Name;}

set {Name=value;}

}

}

(1) Attributes are guaranteed to be secure, and when not used in this class, it is guaranteed that property names can be used to avoid

Use the name of the field.

(2) The set and get functions of a property can limit some of the functions of a field for some purpose.

Such as:

private int a=0;

public int a{Get{return THIS.A;}         set {if (value >= 0 && value <=) This.a=value; else throw new Exception ("The range of values is not valid.     "); }} (3) property does not have the ability to store data, the data exists in the field, so only modifying the field's data can change the data, modifying the value of the property is useless. --------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------

The difference between a property and a field

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.