Monday, 15 March 2010

How can I add \ symbol to the end of string in C# -



How can I add \ symbol to the end of string in C# -

please forgive me beginner's question :)

string s="abc"; s+="\";

won't complile.

string s="abc"; s+="\\";

will create s="abc\\"

how can create s="abc\" ?

your sec piece of code is want (or verbatim string literal @"\" others have suggested), , adds single backslash - print console , you'll see that.

these 2 pieces of code:

s += "\\";

and

s += @"\";

are exactly equivalent. in both cases, single backslash appended1.

i suspect you're getting confused debugger view, escapes backslashes (and other characters). can validate debugger looking @ s.length, you'll see 4 rather 5.

1 note doesn't alter info in existing string, sets value of s refer new string consists of original backslash on end. string objects in .net immutable - that's whole other topic...

c# string escaping

No comments:

Post a Comment