This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What's the # in front of a string in C#?
This is something I have questioned for a long time but never bothered to figure out. When I download third party libraries I have often seen string assignments using a # symbol before the string.
string myString = #"Some text";
But there seems to be absolutely no difference if I simply do
string myString = "Some text";
So what is the # doing?
It is signifies a verbatim string literal and allows you to not have to escape certain characters:
string foo = #"c:\some\path\filename.exe";
vs:
string foo = "c:\\some\\path\\filename.exe";
string reason = #"this string literal mea\ns something different with an # in front than without";
Without the #, the above string would have a new-line character instead of an 'n' in the word "means". With the #, the word "means" looks just like you see it. This feature is especially useful for things like file paths:
string path = #"C:\Users\LookMa\NoDoubleSlashes.txt";
It is a verbatim string literal. It lets you do things like #"C:\" instead of "C:\\", and is especially useful in regex and file paths, since these often use backslashes that shouldn't be parsed by the compiler. See the documentation for more info.
In this case there is no difference.
All '#' does is allow you to omit escape backslashes. If you use '#' and want in include double quotes in the string you need to double up the double quotes.
Related
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What's the # in front of a string for .NET?
I found this in a C# study book
DirectoryInfo dir = new DirectoryInfo(key.Key.ToString() + #":\");
The book however did not explain what the '#' symbol was for. I tried searching MSDN C# Operators but its not listed there. I can guess that it allows the developer to not have to escape a '\' or does it allow to not have any escape sequences?
What is this for and why would I use #":\" instead of ":\\"?
Thanks for the help
Edit: See the comment below for a similar question
It means to interpret the string literally (that is, you cannot escape any characters within the string if you use the # prefix). It enhances readability in cases where it can be used.
For example, if you were working with a UNC path, this:
#"\\servername\share\folder"
is nicer than this:
"\\\\servername\\share\\folder"
It also means you can use reserved words as variable names
say you want a class named class, since class is a reserved word, you can instead call your class class:
IList<Student> #class = new List<Student>();
Prefixing the string with an # indicates that it should be treated as a literal, i.e. no escaping.
For example if your string contains a path you would typically do this:
string path = "c:\\mypath\\to\\myfile.txt";
The # allows you to do this:
string path = #"c:\mypath\to\myfile.txt";
Notice the lack of double slashes (escaping)
As a side note, you also should keep in mind that "escaping" means "using the back-slash as an indicator for special characters". You can put an end of line in a string doing that, for instance:
String foo = "Hello\
There";
What is this for and why would I use #":\" instead of ":\"?
Because when you have a long string with many \ you don't need to escape them all and the \n, \r and \f won't work too.
Perhaps I didn't see or understand any of the answers I read but I am having trouble using verbatim string literal (#) with settings.Default.(mysetting). I am trying to do something like
Directory.GetFiles(#Setting.Default.(mysetting),"*.txt");
and cant seem to find the right syntax to make this work.
The # identifies a string constant literal where back slashes should not be interpreted as escape signs. You can not use it in front of method invocations as you attempt here.
A valid assignment might be
string path = #"c:\temp\example.txt";
Usually a \t would be interpreted as a tabulation character thus making the file reference illegal. It is exactly identical to
string path = "c:\\temp\\example.txt" ;
But bit easier to read.
# verbatim string is used with string literals. So your code should be:
Directory.GetFiles(Setting.Default.(mysetting),#"*.txt");
because "*.txt" is the string literal in your code.
(Although not related, but you can use # with variable names see C# Variable Naming and the # Symbol)
To use # as part of a verbatim string literal, the string literal must be right there - not just a property, method, etc. that returns a string.
string myStr = #"I'm verbatim, I contain a literal \n";
string myStr2 = "I'm not\nI have a newline";
string myStr3 = #myStr2; // still contains a newline, not a literal "\n"
Using # in front of an identifier allows you to use reserved keywords as identifiers. For example:
string #if = "hello!"; // valid
It also works on non-reserved words, where it has no real effect.
string #myVar = "hello!"; // valid
string newVar = myVar; // can be referred to either way
Unless I'm missing it, you still need to wrap the string within quotation marks.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the # in front of a string for .NET?
I have the following code:
new Attachment(Request.PhysicalApplicationPath + #"pdf\" + pdfItem.Value)
What does the # sign do?
It has nothing to do with filepath. It changes the escaping behavior of strings.
In a string literal prefixed with # the escape sequences starting with \ are disabled. This is convenient for filepaths since \ is the path separator and you don't want it to start an escape sequence.
In a normal string you would have to escape \ into \\ so your example would look like this "pdf\\". But since it's prefixed with # the only character that needs escaping is " (which is escaped as "") and the \ can simply appear.
This feature is convenient for strings literals containing \ such as filepaths or regexes.
For your simple example the gain isn't that big, but image you have a full path "C:\\ABC\\CDE\\DEF" then #"C:\ABC\CDE\DEF" looks a lot nicer.
For regular expressions it's almost a must. A regex typically contains several \ escaping other characters already and often becomes almost unreadable if you need to escape them.
It's a verbatim string literal.
This allows the string to contain backslashes and even linebreaks without them being handled differently:
string multiLineString = #"First line
second line
third line";
As backslashes aren't used for escaping, inserting a double quote into the string requires it to be doubled:
string withQuote = #"before""after";
Verbatim string literals are typically used for file paths (as you've shown) and regular expressions, both of which frequently use backslashes.
See my article on strings for more information.
It allows you to enter the backslash (\) without escaping it:
var s1 = "C:\\Temp\\MyFileName";
var s2 = #"C:\Temp\MyFileName";
Both result in a string with the same contents (and since strings are interned at compile time, probably even the same string reference).
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What's the # in front of a string for .NET?
I found this in a C# study book
DirectoryInfo dir = new DirectoryInfo(key.Key.ToString() + #":\");
The book however did not explain what the '#' symbol was for. I tried searching MSDN C# Operators but its not listed there. I can guess that it allows the developer to not have to escape a '\' or does it allow to not have any escape sequences?
What is this for and why would I use #":\" instead of ":\\"?
Thanks for the help
Edit: See the comment below for a similar question
It means to interpret the string literally (that is, you cannot escape any characters within the string if you use the # prefix). It enhances readability in cases where it can be used.
For example, if you were working with a UNC path, this:
#"\\servername\share\folder"
is nicer than this:
"\\\\servername\\share\\folder"
It also means you can use reserved words as variable names
say you want a class named class, since class is a reserved word, you can instead call your class class:
IList<Student> #class = new List<Student>();
Prefixing the string with an # indicates that it should be treated as a literal, i.e. no escaping.
For example if your string contains a path you would typically do this:
string path = "c:\\mypath\\to\\myfile.txt";
The # allows you to do this:
string path = #"c:\mypath\to\myfile.txt";
Notice the lack of double slashes (escaping)
As a side note, you also should keep in mind that "escaping" means "using the back-slash as an indicator for special characters". You can put an end of line in a string doing that, for instance:
String foo = "Hello\
There";
What is this for and why would I use #":\" instead of ":\"?
Because when you have a long string with many \ you don't need to escape them all and the \n, \r and \f won't work too.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What's the # in front of a string for .NET?
Sometimes i saw the sample code, will have a "#" symbol along with the string.
for example:
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlServerCe.3.5";
entityBuilder.ProviderConnectionString = providerString;
entityBuilder.Metadata = #"res://*/App_Data.data.csdl|res://*/App_Data.data.ssdl|res://*/App_Data.data.msl";
On the 4th line, what is that usage of the "#"?
I try to remove this, it still works.
A string literal such as #"c:\Foo" is called a verbatim string literal. It basically means, "don't apply any interpretations to characters until the next quote character is reached". So, a verbatim string literal can contain backslashes (without them being doubled-up) and even line separators. To get a double-quote (") within a verbatim literal, you need to just double it, e.g. #"My name is ""Jon""" represents the string My name is "Jon". Verbatim string literals which contain line separators will also contain the white-space at the start of the line, so I tend not to use them in cases where the white-space matters. They're very handy for including XML or SQL in your source code though, and another typical use (which doesn't need line separators) is for specifying a file system path.
Taken from
It tells the compiler not to treat \ as escape sequences and take the strings literally.