C++ CString equivalent in C#

不打扰是莪最后的温柔 提交于 2019-12-08 17:43:50

问题


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


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/4141796/c-cstring-equivalent-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!