Readonly; static readonly; const

Source: Internet
Author: User

Read-Only fields can only be assigned values during initialization or in constructors. The static readonly field is very similar to the const field. The difference between them is that C # compiler does not compile the value of this static fieldCode(The const value is directly compiled into the code during compilation), but is dynamically parsed at each Runtime (just like treating a common variable ). That is to say, the value of the const field has been determined at compilation, and the value of the static readonly field is determined at runtime.

Constants must be initialized during declaration.

 

You can declare a field as static, indicating that the field is a class attribute rather than an object attribute. This allows the caller to use fields through the class at any time, even if the class does not have any instances.

Note: static classes and class members are used to create class instances to access data and functions. Static class members can be used to separate data and behavior independent of any object representation: No matter what changes occur to the object, these data and functions will not change accordingly. When the class does not depend on the data and behavior represented by objects, you can use static classes.

 

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.