How to use "\" in a string without making its escape sequence - C #?

I am sure that this is something really basic that I do not know, but how to make it not recognize "\" as an escape sequence inside a string

I am trying to enter a path and it considers this to be an escape sequence

+6
c # escaping
source share
6 answers

You can use Verbatim String Literals :

//Initialize with a regular string literal. string oldPath = "c:\\Program Files\\Microsoft Visual Studio 8.0"; // Initialize with a verbatim string literal. string newPath = @"c:\Program Files\Microsoft Visual Studio 9.0"; ↑ 
+42
source share
 string s = @"C:\Temp"; 
+8
source share

It's pretty simple, just do two slashes:

\\

+1
source share

use "\\"

The funny thing is: I had to run \ using \\.

+1
source share

It's just ... Just put the @ symbol in front of your line, then it never cares about your escape sequences ... like this

string name = @ "lndebi \ n \ nlndebi";

the output will be lndebi \ n \ nlndebi.

+1
source share

You can use "\\" instead of "\", as well as the "@" sign at the beginning.

0
source share

All Articles