【发布时间】:2020-05-25 11:50:17
【问题描述】:
出于好奇,有没有办法将 Guid.Empty 声明为 const?我曾经认为所有的值类型都可以声明为常量。
下面的行返回错误:CS0283 The type 'Guid' cannot be declared const
const Guid myConstGuid = Guid.Empty;
【问题讨论】:
-
常量值在编译时应该是“已知的”。
-
const (C# Reference): 常量可以是数字、布尔值、字符串或空引用。
-
为什么需要常量?
Guid.Empty已经起到了只读值的作用。