Field and property in C)

Source: Internet
Author: User

You can access a member variable of a class in two ways: domain and attribute. The domain is accessed as a member variable of the public type, but the attribute cannot be accessed directly. It must be accessed through the accessors.
Field)
Field)
-The field indicates the variables associated with objects or classes.
-If the readonly modifier is added to the declaration of the domain, it indicates that the domain is a read-only domain. For a read-only domain, we can only modify it in the definition of the domain and the constructor of its class. In other cases, the domain is "read-only.
-The role of static readonly is similar to that of # define and const. The difference is that the value of the const expression is formed during compilation, and the value of the static readonly expression is formed only when the program is running. For example:
Public class
{
Public static readonly int X = 1;
}
-Uninitialized variables in C/C ++ cannot be used. In C #, the system provides a default value for each uninitialized variable. For all referenced variables, the default value is null. The default values of all value types are fixed. For static fields, the class is initialized when it is loaded; for non-static fields, it is initialized when the class instance is created. Before the default initialization, the field value is unpredictable.
For example, the following code is valid:
Class Test
{
Static int a = B + 1;
Static int B = a + 1;
}
In fact, it is equivalent to: a = 1; B = 2;
The following code is invalid:
Class
{
Int x = 1;
Int y = x + 1;
}
Because non-static variable x is not initialized before class A is instantiated, code y = x + 1 cannot get the correct value of x.
Property)
-Fully embodies the encapsulation of objects: instead of directly operating the data content, the data is accessed through the accessors, that is, the attribute values are read and written by means of get and set; on the other hand, you can also control the access attribute of data (of course, you can also implement it by adding the readonly keyword to a common domain.

Related Article

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.