.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。之后将bin文件夹中的ConstTest.dll引用文件删除,直接运行Main.exe文件,程序运行正常,结果输出1000。

如果将ConstTest重新定义为:

   1: public class ConstTest
   2: {
   3:     //只能在定义时声明
   4:     public const int ConstInt = 1000;
   5:     
   6:     public readonly int ReadOnlyInt = 100;
   7:     public static int StaticInt = 150;
   8:     
   9:     public ConstTest()
  10:     {
  11:         ReadOnlyInt = 101;
  12:         StaticInt = 151;
  13:     }
  14:     
  15:     //static 前面不可加修饰符
  16:     static ConstTest()
  17:     {
  18:         //此处只能初始化static变量
  19:         StaticInt = 152;
  20:     }
  21: }

重新编译成ConstTest.dll并向调用程序Main添加此引用后,再编译调用程序,生成新的Main.exe,即使再次删除ConstTest.dll文件后,Main.exe运行正常,结果输出1000。

将Main程序更改如下:

   1: class Program
   2: {
   3:     public static void Main(string[] args)
   4:     {
   5:         
   6:         Console.WriteLine(ConstTest.ConstInt);//输出1000
   7:  
   8:         Console.WriteLine(ConstTest.StaticInt);//输出152
   9:  
  10:         ConstTest mc = new ConstTest();
  11:         Console.WriteLine(ConstTest.StaticInt);//输出151
  12:     }
  13: }

重新编译Main程序,如果此时再把ConstTest.dll删除,则会报错。

如此可以看出,如果某些工程引用到了ConstTest.dll,如果后来因为变动,改变了ConstInt常量的值,即使引用重新编译的ConstTest.dll,也无法更改Main.exe中的数据(可以把ConstInt值改为其它值,然后将编译后的ConstTest.dll拷贝到Main.exe的bin文件夹下试试看),这时,只能添加ConstTest.dll引用,并且重新编译Main程序才行。

posted @   Ritchie(乞戈)  阅读(2249)  评论(1编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示