What does the "@" sign on C" mean?

Asked

Viewed 5,144 times

49

I have the following string @"\\servidor01\arquivos". What is the function of @ in front of the string?

4 answers

60


Means a string literal, or a string raw, without considering the escape characters.

The character \ acts as an escape to insert special characters in string, and as it is a network address, you really want this character to be considered.

If you do not use the @, you would have to duplicate the backslash that way:

string a = "\\\\servidor01\\arquivos"

Here are some other examples with the content that the variable will display.

string a = "hello, world";                // hello, world
string b = @"hello, world";               // hello, world
string c = "hello \t world";              // hello     world
string d = @"hello \t world";             // hello \t world
string e = "Joe said \"Hello\" to me";    // Joe said "Hello" to me
string f = @"Joe said ""Hello"" to me";   // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt
string h = @"\\server\share\file.txt";    // \\server\share\file.txt
string i = "one\r\ntwo\r\nthree";
string j = @"one
two
three";

Reference: Microsoft Docs

  • 7

    The term used for strings starting with @ is Verbatim, literal is already any string that is directly in the code delimited by double quotes, whether it’s a regular string or Verbatim, and I wouldn’t say it doesn’t consider escape characters, actually in it the only escape character that exists is the double quote, which should be used two if you want to add one in the text.

29

I have two complements on the subject.

Reserved word

The @ can be used for something else in language. When you need to use an identifier that conflicts with a reserved word, this symbol can be used to indicate that there is an identifier and resolve the ambiguity. Example:

var @lock = true;

This may be necessary for legacy codes and especially when you use a library written in another language where that word is not reserved.

New way to escape a string

In C# 6 there is a new symbol to indicate a special condition of that string. With the addition of interpolation of string, we can use the variable (and maybe an expression) within the string. For this the compiler needs to understand that this is being used in it. It would be so:

var texto = $"Contei {x} vezes";

The $ indicates that the line with {} needs to be resolved at runtime. Read more on the subject.

  • I use the @ to concatenate multiline text, that’s correct?

  • @Marconi I would need to see the shape, but it seems to me that yes, of course this is not exactly concatenation, it’s just a way of writing properly :) Unless you’re concatenating yourself, there you are wrong :D

  • 1

    @Marconi seems suitable, I would only use the $ and put the expressions inside the string, but it feels good: https://answall.com/q/91117/101

2

To better understand the representation of strings in C#, I suggest reading the articles:

String (Reference of C#) and String literals

Briefly, C# supports two types of string representation:

  1. regular string consisting of a string of characters between quotation marks which may contain escape combinations to represent special characters. Escapes combinations are initialized with the backslash (\);

  2. textual string (Verbatim) which consists of a string between quotation marks preceded by the character @. Textual strings do not process escapes combinations.

Examples:

string a = "Hello";           // string regular
string b = @"Hello";          // string textual
string c = "Hello \n World";  // string regular com o escape \n de quebra de linha
string d = @"Hello \n World"; // string textual não processa combinação escape

// strings textuais podem expandir por múltiplas linhas
string e = @"SELECT * 
FROM Funcionario 
WHERE nome LIKE '%Silva%'"; 

0

It is a reserved word of the language, to escape string.

   string arquivo = @"c:\arquivo.txt";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.