类级字符串常量与方法级字符串常量之间是否存在显着差异。编译器会识别常量并应用常量折叠吗?或者总是会创建 nw 对象?
这是示例:类级常量
class A
{
private const string Sid = "sid";
private const string Pid = "pid";
public void Do()
{
Console.WriteLine(Sid);
Console.WriteLine(Pid);
}
}
方法级常量:
class B
{
public void Do()
{
const string Sid = "sid";
const string Pid = "pid";
Console.WriteLine(Sid);
Console.WriteLine(Pid);
}
}