locked
What is the '@' character in front of a string? RRS feed

  • Question

  • i am new to c# and i have often seen a string defined as-->
    string str1=@"helolskdfjlkdsfjl";
    what does the the character '@' mean?
    Saturday, November 5, 2005 5:47 PM

Answers

  • Konstantin,

    That's not actually correct. That line will not compile. You still have to double quote it:



    string message = "This is the first line.\r\nThis is the second line.";

     


    When this message in displayed on screen (like via MessageBox) all text after '\r\n' is displayed on a new line.

    Using the @ character if front on the string literal, causes the escape sequence to be ignored:


    string message = @"This is the first line.\r\nThis is the second line.";

     


    When this message is displayed, the entire text appears on the same line, and displays exactly how it appears in the text, including the '\r\n'.

    The @ symbol also makes write file name paths easier, for example, to hard code a path within a string literal you would need to do the following:



    string path = "c:\\Folder1\\Folder2\\Folder3\\MyFile.txt";

     

    Compare that to this:


    string path = @"c:\Folder1\Folder2\Folder3\MyFile.txt";

     

    Hope that helps.

    David

    Saturday, November 5, 2005 10:39 PM

All replies

  • If you have symbols like " in your string, say

    string str1= "Just a "title" of a string"; //wont compile

    that would be an incorect syntax for compilier - you can't write " inside of "".  But if you place @ in the front of it, then it's a valid expression

    string str1= @"Just a "title" of a string"; //will compile
    Saturday, November 5, 2005 5:55 PM
  • Konstantin,

    That's not actually correct. That line will not compile. You still have to double quote it:



    string message = "This is the first line.\r\nThis is the second line.";

     


    When this message in displayed on screen (like via MessageBox) all text after '\r\n' is displayed on a new line.

    Using the @ character if front on the string literal, causes the escape sequence to be ignored:


    string message = @"This is the first line.\r\nThis is the second line.";

     


    When this message is displayed, the entire text appears on the same line, and displays exactly how it appears in the text, including the '\r\n'.

    The @ symbol also makes write file name paths easier, for example, to hard code a path within a string literal you would need to do the following:



    string path = "c:\\Folder1\\Folder2\\Folder3\\MyFile.txt";

     

    Compare that to this:


    string path = @"c:\Folder1\Folder2\Folder3\MyFile.txt";

     

    Hope that helps.

    David

    Saturday, November 5, 2005 10:39 PM
  • Good explanation! Idea
    Saturday, November 5, 2005 10:49 PM
  • thanks man that helped a lot.actually that was a nice explaination
    Sunday, November 6, 2005 2:57 AM