String.Concat & Newline Character

  • Thread starter Thread starter rlm
  • Start date Start date
R

rlm

Newbie question: attempting the following...

userName = String.Concat(Environment.UserDomainName,
"\" ,Environment.UserName);

The error is "New Line in constant".

I tried figuring out the escape character - but I am and will be a
Newbie in C# for the next 4 - 6 months.
 
Newbie question: attempting the following...

userName = String.Concat(Environment.UserDomainName,
"\" ,Environment.UserName);

The error is "New Line in constant".

I tried figuring out the escape character - but I am and will be a
Newbie in C# for the next 4 - 6 months.

The backslash is the escape character, so when you want a literal backslash
in a string you have two choices:

1) Escape it: "\\" equates to a single backslash.

2) Prefix the entire string with an @ sign: @"\". Note that this turns off
ALL escaping, so you could no longer add, for example, a line feed to the
text like this: @"Line 1\nLine2". This will result in the string

Line 1\nLine2

and NOT

Line 1
Line2
 
Back
Top