Resolved Problem with verbatim string

Sajo

Member
Joined
Jul 22, 2020
Messages
17
Programming Experience
Beginner
Hello everybody! I have a problem with verbatim string. I want to write this "c:\\projects\\project1\\enter"
I use verbatim string for that. But when I run console it always display me c:\projects\project1\enter. I want to write code which is easier to read.
I want double backslash not just one. In the tutorial, they said that it is enough to use the verbatim string for this case where i need double backslash not just one.
Is the verbatim string no longer used in C# or what?
C#:
namespace aplikacija
{
    class Program
    {
        static void Main(string[] args)
        {
            string name = @" c:\projects\project1\enter";
            Console.WriteLine(name);
        }
    }
}
 
Last edited by a moderator:
And how many back slashes do you have in your string per each backslash?

One, so why would you expect there to be two?
 
The whole point of a verbatim string literal is that every character is literal. If you expect to see two slashes in your string then you have to put two slashes in, not one:
C#:
string name = @"c:\\projects\\project1\\enter";
 
That said, why would you want to write that in the first place? Maybe there's a valid reason but I can't think of one off the top of my head, other than maybe writing a code file.
 
Right now, the only thing I can think off based on the popularity of JSON is that our OP is trying to generate JSON so they need to have the escaped backslashes in the output.
 
Hahaha I am beginner in all of this. I watch the course for CSharp so this was example on my course. The lecturer said that in this way I can get doubles backslashes. I tried his way and I could not got that . Lecturer does not explained verbatim string very well. Thank you so much guys. You helped me a lot
 

Use MSDN for researching your course work. If you are still having problems, you can always post here and we can guide you if there is something you don't understand.
 
Without verbatim string you have to write "c:\\projects\\project1\\enter" to get "c:\projects\project1\enter" ;) The first backslash is an escape character.
Watch the output of this code:
C#:
Console.WriteLine("c:\\projects\\project1\\enter");
Console.WriteLine(@"c:\projects\project1\enter");
 

Latest posts

Back
Top Bottom