What is the C#
equivalent for MFC's CString
?
Asked
Active
Viewed 9,719 times
8

Billy ONeal
- 104,103
- 58
- 317
- 552

Thomas Anderson
- 1,977
- 7
- 17
- 22
-
4`CString` is an MFC class which is not part of standard C++. I have updated the question accordingly. – Billy ONeal Nov 10 '10 at 06:32
3 Answers
10
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 ofSystem.String
as aconst 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 usenew 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.

cdhowie
- 158,093
- 24
- 286
- 300
-
+1 -- though I think you mean StringBuilder rather than StringBuffer. – Billy ONeal Nov 10 '10 at 06:34
-
1`"most coders will LART you if you use the uppercase form"` what is LART btw? – Inverse Nov 10 '10 at 07:04
-
1
2
You can try to use String, and see if it helps.

Adrian Fâciu
- 12,414
- 3
- 53
- 68
-
1Note that this has additional support in the language in the form of the (lowercase) `string`. – Billy ONeal Nov 10 '10 at 06:33
2
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.

Pontus Gagge
- 17,166
- 1
- 38
- 51