Defining the @ symbol prior to the assigning a string value will prevent the need for doubling the backslash (\) in C#. The at symbol (@) simply ignores escape characters. This is useful when accessing a directory local to the server. For example, you may want to store the local directory path in a string. Without using the at symbol (@), it would look similar to the following:
string directoryPath = “C:\\uploadedFiles\\pdfs”;
Note that in the above example, each backslash is needs to be doubled. However, with the at symbol (@), it is both easier to read and type because we can keep the original single backslash format.
string directoryPath = @“C:\uploadedFiles\pdfs”;
Just another trick in C# that may not be blatantly obvious, but very useful. Note that using the at symbol (@) can can be used sparing as in the demonstrated example below:
string directoryPath = “C:\\uploadedFiles” + @“\pdfs”;
In the example above, the at symbol (@) was not used in the first half, but was used in the second half. Not sure why you would use this third example in this case, but just remember that it is an option.
This entry was posted on Thursday, April 10th, 2008 at 8:19 am and is filed under C#. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply