Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use @ in c#?

Tags:

c#

I use @ symbol with local path only, but when do I use @ exactly?

like image 358
Moon Avatar asked Jun 29 '09 11:06

Moon


People also ask

When %s is used in C?

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.

Why #is used in C?

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.

Why do we use semicolon in C?

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.

What does %d do in C?

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.


4 Answers

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."
like image 178
ChrisF Avatar answered Oct 17 '22 02:10

ChrisF


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.

like image 25
Aamir Avatar answered Oct 17 '22 00:10

Aamir


Verbatim Strings

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";

Reserved Words

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.

like image 45
Rob Avatar answered Oct 17 '22 01:10

Rob


If you want to use keywords as variable names

string @string = "Hi";
like image 42
Sauron Avatar answered Oct 17 '22 01:10

Sauron