62人参与 • 2026-05-10 • Asp.net
在 c# 编程中,const 与 readonly 经常被统称为“常量”,但二者在初始化规则、编译/运行时行为、il 生成方式、版本兼容性、引用类型语义等方面存在本质差异。误用不仅可能引入隐蔽的逻辑错误,还会带来库升级后的版本陷阱。
public class constantdemo
{
public const int maxretrycount = 3;
public const string defaulttitle = "c#常量解析";
// ❌ 编译错误:声明时未赋值
// public const double pi;
// ❌ 编译错误:不能在构造函数中修改
// public constantdemo()
// {
// maxretrycount = 5;
// }
}
📌 结论:const 是“声明即终值”的编译期常量。
public class readonlydemo
{
// 声明时赋值
public readonly int minage = 18;
// 构造函数中赋值
public readonly int userid;
public readonlydemo(int userid)
{
userid = userid;
}
// 静态 readonly:在静态构造函数中赋值
public static readonly string version;
static readonlydemo()
{
version = "1.0.1";
}
}
📌 结论:readonly 是“构造期冻结”的运行时常量。
public class constscopedemo
{
public const int globalconst = 100;
public void localconstdemo()
{
const string localmsg = "局部常量";
console.writeline(localmsg);
}
}
public class readonlyscopedemo
{
public readonly int fieldreadonly = 50;
public void localreadonlyerror()
{
// ❌ 编译错误:readonly 不能修饰局部变量
// readonly int x = 10;
}
}
public const int constvalue = 10;
public void useconst()
{
int a = constvalue;
}
il 行为本质:
ldc.i4.s 10 // 直接压栈常量 10
⚠️ 重大隐患(版本陷阱):
- 修改类库中的 const 值
- 但 引用方未重新编译
- 引用方仍然使用旧值 ❌
public readonly int readonlyvalue;
public readonlydemo()
{
readonlyvalue = 20;
}
public void usereadonly()
{
int b = readonlyvalue;
}
il 行为本质:
ldfld int32 readonlyvalue
✅ 修改值后,只需重新编译类库即可,调用方无需重新编译。
public class conststaticdemo
{
public const int conststatic = 10;
// ❌ 编译错误
// public static const int invalid = 20;
}
调用方式:
int x = conststaticdemo.conststatic;
public class readonlystaticdemo
{
public readonly int instancereadonly = 100;
public static readonly int staticreadonly = 200;
}
public class constreferencedemo
{
public const string conststring = "hello";
public const object constnull = null;
// ❌ 编译错误
// public const list<int> constlist = new list<int>();
}
原因:
const 需要 编译期确定值string 外,引用对象无法编译期确定public class readonlyreferencedemo
{
public readonly list<int> numbers = new() { 1, 2, 3 };
public void modify()
{
numbers.add(4); // ✅ 合法
// ❌ 编译错误:不能重新赋值
// numbers = new list<int>();
}
}
⚠️ readonly ≠ 不可变对象
- 锁的是 引用地址
- 不是对象内容
| 维度 | const | readonly |
|---|---|---|
| 初始化时机 | 编译期 | 运行期 |
| 赋值位置 | 仅声明处 | 声明 / 构造函数 |
| 修饰对象 | 字段 + 局部变量 | 仅字段 |
| 静态特性 | 默认 static | 默认实例级 |
| il 行为 | 内联常量 | 字段访问 |
| 引用类型 | string / null | 任意引用类型 |
| 版本安全 | ❌ 易出问题 | ✅ 安全 |
pi、e)public const int maxdays = 7;
public static readonly string connectionstring;
static dbconfig()
{
connectionstring = loadfromconfig();
}
const 是“编译期写死的字面量”
readonly 是“构造期冻结的字段”
const 与 readonly 的差异,本质并不在“能不能改”,而在于:
在真实工程中:
🔥 99% 对外暴露的“常量”,都应该使用 readonly 而不是 const。
理解这一点,你就已经超过了大多数只停留在语法层面的 c# 开发者。
到此这篇关于浅谈c# 中 const 与 readonly的核心区别的文章就介绍到这了,更多相关c# const readonly内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论