当前位置:编程学习 > C#/ASP.NET >>

.NET 中的 常量字段const

C#中,当使用常数符号const时,编译器首先从定义常数的模块的元数据中找出该符号,并直接取出常数的值,然后将之嵌入到编译后产生的IL代码中,所以常数在运行时不需要分配任何内存,当然也就无法获取常数的地址,也无法使用引用了。

如下代码:

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

将其编译成ConstTest.dll文件,并在如下代码中引用此ConstTest.dll文件。

   1: using System;
   2:  
   3: class Program
   4: {
   5:     public static void Main(string[] args)
   6:     {
   7:         Console.WriteLine(ConstTest.ConstInt);//结果输出为1000;
   8:     }
   9: }

编译运行此Main.exe程序,结果输出为1000。之后将bi

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,