Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ CString equivalent in C#

Tags:

c++

c#

mfc

cstring

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

like image 268
Thomas Anderson Avatar asked Nov 10 '10 06:11

Thomas Anderson


2 Answers

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.

like image 148
cdhowie Avatar answered Sep 18 '22 10:09

cdhowie


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

like image 21
Adrian Fâciu Avatar answered Sep 21 '22 10:09

Adrian Fâciu