Multi-line String Literal in C#

String literal can be created  prefixed with @.
String literal does not require any escape character except double quote.

1. The most precious and special super power of a string literal is its multi-line ability.
i). Instead of:
             var += @”some text” + Environment.NewLine;
             var += @”some more text” + Environment.NewLine;
             var += @”even more text.” + Environment.NewLine;

     You can use:
            var = @”some text
                        some more text

                        even more text.”;

ii). Instead of:
           query =  ” SELECT foo, bar”
                    + ” FROM table”

                    + ” WHERE id = 42″;

           query = @”SELECT foo, bar
                       FROM table

                       WHERE id = 42″;

i) You can use the backslash as a backslash.
          path =    “c:\\Windows\\SomeDirectory\\”;
          path = @”c:\Windows\SomeDirectory\”;

ii) Double quote required two quotes to mean a quote.
         string = “String literal \”1\””;     //output- String literal “1”
         string = @”String literal “”2″””;   //output- String literal “3”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s