gpt4 book ai didi

c# - 有没有办法将字符串与 char 作为 const 连接?

转载 作者:太空狗 更新时间:2023-10-30 00:26:48 26 4
gpt4 key购买 nike

如果我尝试

const char NoChar = (char)8470; //№
const char TmChar = (char)8482; //™
const string IdDisplayName = "Clements" + TmChar + ' ' + NoChar;

它会抛出一个编译错误:

The expression being assigned to '{0}' must be constant

据我了解,发生此错误是因为当一个 char 是因为字符串连接运算符 (+) 在内部调用连接对象上的 ToString

我的问题是是否有办法(非托管?Tongue)做到这一点。

我需要将该常量作为属性传递,它应该在客户端生成。

更丑陋的解决方法(将根据您的答案查看更丑陋的东西......)是对该属性进行子类化(它是密封的,必须进行一些反编译和复制粘贴工作)并将其作为非常量嵌入有可能。

最佳答案

您可以通过 \u 转义符直接在字符串中指定 unicode 字符值。所以 const string IdDisplayName = "Clements\u2122\u2116"; 应该能满足您的需求。

关于c# - 有没有办法将字符串与 char 作为 const 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9460345/

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