I use @ symbol with local path only, but when do I use @ exactly?
Note: The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to print and read strings directly.
In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more. It actually adds code to the source file before the final compilation.
Semicolons are end statements in C. The Semicolon tells that the current statement has been terminated and other statements following are new statements. Usage of Semicolon in C will remove ambiguity and confusion while looking at the code.
In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.
You use @ before strings to avoid having to escape special characters.
This from the MSDN:
Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths. Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to embed a quotation mark inside a verbatim string. The following example shows some common uses for verbatim strings:
string filePath = @"C:\Users\scoleridge\Documents\"; //Output: C:\Users\scoleridge\Documents\
string text = @"My pensive SARA ! thy soft cheek reclined
Thus on mine arm, most soothing sweet it is
To sit beside our Cot,..."; /* Output: My pensive SARA ! thy soft cheek reclined Thus on mine arm, most soothing sweet it is To sit beside our Cot,...
*/
string quote = @"Her name was ""Sara."""; //Output: Her name was "Sara."
AFAIK, You can use @ at any place where you don't want to let the default meaning of the thing persist. For example, @class will make class an identifier. @bool will make bool an identifier instead of a keyword.
You know the usage of @ before strings. It is used to indicate that take all the text of the string literally and don't treat any character in the string specially.
Edit: An yes, another thing to this is that @Keyword
is compiled into a Keyword
in IL.
See this Link for more details.
An @
before a string literal in C# denotes a verbatim string. In a verbatim string, only the quote escape sequence (""
) is parsed as an escape sequence; all others, e.g. \n
, \t
etc. are ignored.
You'll have seen this syntax used with file paths, etc. because it's convenient to have the compiler ignore the backslashes in the path, rather than having to double-escape them, e.g.
var s = @"c:\Some\File\Path.txt";
is a little easier to read than
var s = "c:\\Some\\File\\Path.txt";
You can also prefix identifiers with @
in order to allow using reserved words in identifiers. For example, @class
can be used as an identifier, whereas class
wouldn't be permitted. In this specific case, @class
is a little less jarring (at least, I find) than the usual convention of klass
or clazz
which are often used to work around this restriction in other languages.
If you want to use keywords as variable names
string @string = "Hi";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With