Valid tive C # Item 2: prefer readonly to const

Source: Internet
Author: User

Valid tive C # Item 2: prefer readonly to const

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 constant
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 following program segments 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.

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.

Translated from Objective C #: 50 specific ways to improve your C # by Bill Wagner

For some areas of compile-time constants, it is translated as "compile-time ". Because it has static characteristics, I still use static constants. For runtime constants, it is equivalent to a read-only data member that can be modified in the class constructor.

 

Static Constants

Dynamic constant

Memory consumption

None

Because the consumption of constants to be saved

Initialization

Few simple types, not new, must be assigned at the same time

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

Back to directory

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.