Static constants and Dynamic Constants (const, static, readonly)

Source: Internet
Author: User
Today I want to write a static constant, and I will write it:
Public static const int pagesize = 15;
When the result is compiled, an error occurs: static and const cannot be used together. Oh, originally, const is an implicit static statement and does not need to be limited by static.
Then I searched the internet and found: (from: http://hi.baidu.com/zzh_my/blog/item/f86c0d23e052aa5b9922ed1c.html)
C # has two different constants: compile-time constants and runtime constants ). They have different features. Incorrect use may not only result in loss of efficiency, but also cause errors. In contrast, static constants are slightly faster in terms of speed, but more flexible than dynamic constants.

// Static constants (implicitly static)
Public const int compiletimeconstant = 1;
// Dynamic constant
Public static readonly runtimeconstant = 1;

Static constants are replaced with the corresponding values during compilation. That is to say, the following two statements generate the same il after compilation by the compiler.

// After compilation, the two are translated into the same intermediate language.
Int mynum = compiletimeconstant;
Int mynum = 1;

The value of a dynamic constant is obtained at runtime. Il marks it as a read-only constant instead of a constant value.

Static constants can only be declared as simple data types (built-in int and floating point types), enumerations, or strings. The followingProgramSegments cannot be compiled. You cannot use the new keyword to initialize a static constant, even for a value type.

// This is incorrect.
Public const datetime mydatetime = new datetime );
// This is acceptable
Public static readonly datetime mydatetime = new datetime );

Read-only data is also a constant and cannot be modified after the constructor initialization. However, unlike static constants, the value is assigned at run time, which gives more flexibility. Dynamic Constants can be any data type.

The biggest difference between the two is that static constants are converted into corresponding values during compilation, which means that for different assembly, when you change a static constant, you need to re-compile it. Otherwise, the constant value will not change, which may lead to potential problems, but dynamic constants will not.

A constant defined by const (which is implicitly static) needs to access the constant defined by const just like a static member, and a compilation error occurs when accessed by object members. You must set the constant value when declaring.
On the other hand, if you want to declare constants that never change and are unique everywhere, such as the idhook parameter of the hook function setwindowshookex or the version during serialization, you should use static constants. However, there are not many opportunities to use such constants. Generally, we should use dynamic constants with higher flexibility.

Metric item

Static Constants

Dynamic constant

Memory consumption

None

Because the consumption of constants to be saved

initialize

few simple types, it cannot be New , the value must be

Any type. values can be assigned to the class constructor.

When to play a role

Replace during compilation

Equivalent to a data member in a class

the above information shows the differences between const, static, and readonly.

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.