C++ C# 中的 CString 等效项

发布于 2024-10-01 08:05:57 字数 59 浏览 0 评论 0原文

MFC 的 CStringC# 等效项是什么?

What is the C# equivalent for MFC's CString?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奢望 2024-10-08 08:05:57

可能是System.String。但为了提供更多有用的信息:

  • System.String 实例是不可变的。连接/子字符串/等实际上创建了新的字符串对象,因此使用字符串实例作为构建输出的缓冲区是一个非常糟糕的主意,如果您打算这样做的话。将 System.String 视为 const CString
  • System.Text.StringBuilder 用于构建和操作字符串内容。它有一个 .ToString() 方法,您可以调用它来将其内容转换为正确的字符串。
  • 如果您确切知道生成的字符串将有多长,则可以使用 char[] 作为字符串生成器的替代方案,但即使如此,您也可以使用 new StringBuilder(length) 指定默认初始容量。然后,您可以使用相关的追加方法,而不必保留索引变量。 (StringBuilder 会为您完成此操作。)

正如 Billy 在该问题的另一个答案中提到的,C# 有一个关键字 string,它本质上是 System.String 的快捷方式。字符串类型。两者完全等效,但如果您使用大写形式,大多数程序员都会对您提出警告。

Probably System.String. But in an effort to provide more useful information:

  • System.String instances are immutable. Concatenation/substring/etc actually create new string objects, so using a string instance as a buffer for building up output is a really bad idea, in case you were going to do that. Think of System.String as a const CString.
  • System.Text.StringBuilder is used to build up and manipulate string content. It has a .ToString() method you can call to turn its contents into a proper string.
  • You can use char[] as a sort of string builder alternative, if you know exactly how long a generated string will turn out to be, but even so you can use new StringBuilder(length) to specify a default initial capacity. You can then use the relevant append methods without having to keep around an index variable. (StringBuilder does that for you.)

As Billy mentioned in the other answer to this question, C# has a keyword string that is essentially a shortcut to the System.String type. Both are completely equivalent, though most coders will LART you if you use the uppercase form.

司马昭之心 2024-10-08 08:05:57

您可以尝试使用String,看看是否有帮助。

You can try to use String, and see if it helps.

如果没有 2024-10-08 08:05:57

您知道,CString 并不是正确完成 OO 设计的光辉示例。 MS 不断地为其添加会员功能,直到它像一把瑞士军刀……但仍然缺少重要的功能。

使用 CString 可以做的任何事情都可以通过不可变的“System.String”和许多支持类来更好地完成。

You know, CString ain't exactly a shining example of OO design done right. MS kept adding member functions to it until it resembled a Swiss army knife... while still missing impotant functions.

Anything you can do with a CString can be better done with the immutable 'System.String` and a number of support classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文