gpt4 book ai didi

c# - 为什么我不能将不可打印的字符定义为常量 c#

转载 作者:行者123 更新时间:2023-11-30 13:54:42 25 4
gpt4 key购买 nike

private const string requireNonPrintableChar = new string('\x0005', 1);

我不希望这个值改变。曾有人告诉我,对您不希望更改的值使用 const 是一种很好的做法。

但是 msbuild 说:“错误 1 ​​分配给 requireNonPrintableChar 的表达式必须是常量”

我假设 char 不算作常量,但为什么呢?

msdn 说它可以是一个字符串,我相信它是。

最佳答案

简单地做:

private const string requireNonPrintableChar = "\x0005";

new string('\x0005', 1) 是一个表达式,确实被评估为字符串,但它不是编译时常量,并且 const 字段只能赋值,这些值可以在编译时求值。

关于c# - 为什么我不能将不可打印的字符定义为常量 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39827557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com