Some differences between static constants (const) and Dynamic Constants (static readonly) in C #

Source: Internet
Author: User

C # contains two constants: static constants and Dynamic Constants. Proper exploitation can improve the program efficiency. Improper exploitation can increase the overhead and even cause errors.

Static constant: const (implicitly static)

For example, public const int compiledata = 1;

Dynamic constant: static readonly

For example, public static readonly runtimedate = 1;

Static constants are replaced with corresponding values during compilation, which is equivalent

Int value = compiledate;

Int compiledata = 1;

Static constants do not consume memory, but only support native data types.

Dynamic Constants are obtained at runtime, so the following statement is correct:

Public static readonly Hello = new Hello ();

Therefore, dynamic constants have greater flexibility, but they need to be saved. Therefore, memory consumption occurs and any type of constants is supported.

These are some tips of the monks. Please add and correct them.

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.