C#中,当使用常数符号const时,编译器首先从定义常数的模块的元数据中找出该符号,并直接取出常数的值,然后将之嵌入到编译后产生的IL代码中,所以常数在运行时不需要分配任何内存,当然也就无法获取常数的地址,也无法使用引用了。
如下代码:
复制代码 代码如下:
public class ConstTest
{
public const int ConstInt = 1000;
}
将其编译成ConstTest.dll文件,并在如下代码中引用此ConstTest.dll文件。
复制代码 代码如下:
using System;
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(ConstTest.ConstInt);//结果输出为1000;
}
}
编译运行此Main.exe程序,结果输出为1000。之后将bin文件夹中的ConstTest.dll引用文件删除,直接运行Main.exe文件,程序运行正常,结果输出1000。
如果将ConstTest重新定义为:
复制代码 代码如下:
public class ConstTest
{
//只能在定义时声明
public const int ConstInt = 1000;
public readonly int ReadOnlyInt = 100;
public static int StaticInt = 150;
public ConstTest()
{
ReadOnlyInt = 101;
StaticInt = 151;
}
//static 前面不可加修饰符
static ConstTest()
{
//此处只能初始化static变量
StaticInt = 152;
}
}
重新编译成ConstTest.dll并向调用程序Main添加此引用后,再编译调用程序,生成新的Main.exe,即使再次删除ConstTest.dll文件后,Main.exe运行正常,结果输出1000。
将Main程序更改如下:
复制代码 代码如下:
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(ConstTest.ConstInt);//输出1000
Console.WriteLine(ConstTest.StaticInt);//输出152
ConstTest mc = new ConstTest();
Console.WriteLine(ConstTest.StaticInt);//输出151
}
}
重新编译Main程序,如果此时再把ConstTest.dll删除,则会报错。
如此可以看出,如果某些工程引用到了ConstTest.dll,如果后来因为变动,改变了ConstInt常量的值,即使引用重新编译的ConstTest.dll,也无法更改Main.exe中的数据(可以把ConstInt值改为其它值,然后将编译后的ConstTest.dll拷贝到Main.exe的bin文件夹下试试看),这时,只能添加ConstTest.dll引用,并且重新编译Main程序才行。
常量字段,const
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 纯音入心系列纯音乐《中央民族乐团-古筝传奇》1CD[MP3][253.1MB]
- 江玲.1989-这样你才爱我,是吗?【新作有声】【WAV+CUE】
- 郭美美.2010-我是郭美美【华纳】【WAV+CUE】
- 黄心懋1991-轻忧郁【滚石】【WAV+CUE】
- 群星.2000-大地金曲世纪回顾2CD【大地】【WAV+CUE】
- 王菲.1996-浮躁(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 群星.2022-传家电视剧原声带【东阳欢娱】【FLAC分轨】
- 《摇滚教父 伍佰黄金精选 2CD》 [WAV+CUE][1GB]
- 《凤凰传奇 我从草原来》[WAV/分轨][400MB]
- 《叶倩文 真心真意过一生》[WAV+CUE][400MB]
- 潘越云.1985-世间女子(滚石25周年经典复刻版)【滚石】【WAV+CUE】
- 陈柏宇.2009-CANT.BE.HALF【SONY】【WAV+CUE】
- 李明依.1989-小女生【滚石】【WAV+CUE】
- 《李荣浩 新时代创作新人王》[WAV分轨][310MB]
- 《黑鸭子与马头琴 史上最动听草原音乐》[WAV+CUE][320MB]