0

I just want to write in Console this "\".

Console.Write("\"); 

But it doesn't recognize it like a string or character ,but as a command.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 12
    No, it recognizes it as an escape character. Either use `Console.Write("\\")` or `Console.Write(@"\")`. This is just a matter of string literals. – Jon Skeet Feb 27 '17 at 18:04
  • It's the one that goes from upper left to lower right. Can't miss it. – 15ee8f99-57ff-4f92-890c-b56153 Feb 27 '17 at 18:05
  • If there is nothing in your string to escape (ie. you want the literal value inside the double quotes) then prefix it with `@`. Example: `Console.Write(@"\");` – Igor Feb 27 '17 at 18:07

3 Answers3

3

\ is used as an escape character inside strings. In order to output the \ itself you need to escape it by doubling it up, or use a string literal by prefixing your string with @.

Either of these will work.

Console.WriteLine("\\");

Console.WriteLine(@"\");
Jamiec
  • 133,658
  • 13
  • 134
  • 193
2

'\' is an escape character. Use Console.WriteLine("\\") to get the desired output. You can also use @ like Console.WriteLine(@"Your_Content") and the content will be automatically escaped.

tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
1

It's a escape character, use Console.Write("\\"). These are used in escape sequences, here is a list of them:

  • \a → Bell (alert)
  • \b → Backspace
  • \f → Formfeed
  • \n → New line
  • \r → Carriage return
  • \t → Horizontal tab
  • \v → Vertical tab
  • \' → Single quotation mark
  • \" → Double quotation mark
  • \\ → Backslash (You have to use this one)
  • \? → Literal question mark
  • \ ooo → ASCII character in octal notation
  • \x hh → ASCII character in hexadecimal notation
  • \x hhhh → Unicode character in hexadecimal notation if this escape sequence is used in a wide-character constant or a Unicode string literal.
  • \uxxxx → Unicode escape sequence for character with hex value xxxx
  • \xn[n][n][n]→ Unicode escape sequence for character with hex value nnnn (variable length version of \uxxxx)
  • \Uxxxxxxxx → Unicode escape sequence for character with hex value xxxxxxxx (for generating surrogates)

You can also use Console.WriteLine(@"\");, see this for an expanation.

Community
  • 1
  • 1
Juan T
  • 1,219
  • 1
  • 10
  • 21