. NET constant field const

Source: Internet
Author: User

In C #, when the constant symbol const is used, the compiler first finds this symbol from the metadata of the module defining the constant and directly retrieves the constant value, then embed it into the compiled IL code. Therefore, the constant does not need to be allocated any memory during runtime. Of course, the constant address cannot be obtained or referenced.

The following code:

   1: public class ConstTest
   2: {
   3:     public const int ConstInt = 1000;
   4: }

Compile it into a ConstTest. dll file and reference this ConstTest. dll file in the following code.

   1: using System;
   2:  
   3: class Program
   4: {
   5:     public static void Main(string[] args)
   6:     {
7: Console. WriteLine (ConstTest. ConstInt); // The result output is 1000;
   8:     }
   9: }

Compile and run the main.exe program, and the output is 1000. Then

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.