A constant in C # may cause version update problems!

Source: Internet
Author: User

A constant is a symbol that represents a constant value. When defining a constant, we must determine its value at compilation. When using a constant, the compiler first looks for the symbol from the module metadata that defines the constant, directly retrieves the value of the constant, and then embeds it into the compiled IlCode. Since the constant value is directly embedded in the Code, the constant does not need any memory allocation during runtime, so we cannot get the constant address, or a constant is passed as a reference.

In the following example, we will find that constants may cause version update problems:

 

Using System;
Public   Class Component
{
Public   Const Int32 maxentriesinlist = 50 ;
}

 

 

Reference thisProgramSet:

 

Using System;
Class App
{
Static   Void Main ()
{
Console. writeline ("Max entries supported In List :" + Component. maxentriesinlist );
}
}

 

 

The above program references the maxentriesinlist constant. When the application is compiled, the compiler will find that maxentriesinlist is a constant symbol with a value of 50, the integer 50 is directly embedded into the application's il code. This will cause the application to run normally even if the referenced DLL assembly does not exist after compilation. we can delete component. dll after compilation is successful.

This means that if we change the maxentriesinlist in the previous component assembly to 1000, and re-compile maxentriesinlist. after DlLL, the app will not perceive all of this, because it will not go to maxentriesinlist again. DLL to obtain the value of the maxentriesinlist constant, unless we re-compile the app, this is a problem that must be noted and understood when using constants.

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.