Using @ Sign at String Variables?

  • Thread starter Thread starter Axel Dahmen
  • Start date Start date
A

Axel Dahmen

Hi,

one of my colleagues has written something like the following code in C#:

string a = @ConfigurationManager.AppSettings["MyTempPath"];

Is the @ sign of any use here?

Or is it just the compiler not giving an error because it's basically a string that's being attached to the @ sign?

Thanks for enlightening me!

Regards,
www.axeldahmen.de
Axel Dahmen
 
Axel,

The @ operator is of no use here. @ has two significant uses in C#:

1) Hard-coded string literals, which start with @ do not have their escape
sequences processed. For example, this allows you to avoid escaping file
paths (@"C:\folder\path.txt" vs "C:\\folder\\path.txt") but the disadvantage
is that you can't use any escape sequences such as \n (new line) or \r
(carriage return).

2) @ can be used for variable names shared with such C# keywords as 'new',
'class', etc. e.g.

string class = "test"; // Identifier expected, 'class' is keyword
string @class = "test"; // OK

HTH,
--
Stanimir Stoyanov
Microsoft MVP -- Visual C#

http://stoyanoff.info
Hi,

one of my colleagues has written something like the following code in C#:

string a = @ConfigurationManager.AppSettings["MyTempPath"];

Is the @ sign of any use here?

Or is it just the compiler not giving an error because it's basically a
string that's being attached to the @ sign?

Thanks for enlightening me!

Regards,
www.axeldahmen.de
Axel Dahmen
 
Axel,

The @ operator is of no use here. @ has two significant uses in C#:

1) Hard-coded string literals, which start with @ do not have their escape
sequences processed. For example, this allows you to avoid escaping file
paths (@"C:\folder\path.txt" vs "C:\\folder\\path.txt") but the disadvantage
is that you can't use any escape sequences such as \n (new line) or \r
(carriage return).

2) @ can be used for variable names shared with such C# keywords as 'new',
'class', etc. e.g.

string class = "test"; // Identifier expected, 'class' is keyword
string @class = "test"; // OK

HTH,
--
Stanimir Stoyanov
Microsoft MVP -- Visual C#

http://stoyanoff.info
Hi,

one of my colleagues has written something like the following code in C#:

string a = @ConfigurationManager.AppSettings["MyTempPath"];

Is the @ sign of any use here?

Or is it just the compiler not giving an error because it's basically a
string that's being attached to the @ sign?

Thanks for enlightening me!

Regards,
www.axeldahmen.de
Axel Dahmen
 
Hi, Stanimir,

great, thanks. So there is no use for it with string variables, as I thought.

That variable name behaviour was new to me. Thanks for enlighten me on this one!

Best regards,
www.axeldahmen.de
Axel Dahemn
 
Hi, Stanimir,

great, thanks. So there is no use for it with string variables, as I thought.

That variable name behaviour was new to me. Thanks for enlighten me on this one!

Best regards,
www.axeldahmen.de
Axel Dahemn
 
Hi Axel,

In addition to Stanimir's second point, here I quote the C# 3.0
specification 2.4.2 in case you and other community members who are
interested:

The prefix "@" enables the use of keywords as identifiers, which is useful
when interfacing with other programming languages. The character @ is not
actually part of the identifier, so the identifier might be seen in other
languages as a normal identifier, without the prefix. An identifier with an
@ prefix is called a verbatim identifier. Use of the @ prefix for
identifiers that are not keywords is permitted, but strongly discouraged as
a matter of style.

The example:
class @class
{
public static void @static(bool @bool) {
if (@bool)
System.Console.WriteLine("true");
else
System.Console.WriteLine("false");
}
}
class Class1
{
static void M() {
cl\u0061ss.st\u0061tic(true);
}
}
defines a class named "class" with a static method named "static" that
takes a parameter named "bool". Note that since Unicode escapes are not
permitted in keywords, the token "cl\u0061ss" is an identifier, and is the
same identifier as "@class".

Two identifiers are considered the same if they are identical after the
following transformations are applied, in order:
" The prefix "@", if used, is removed.
" Each unicode-escape-sequence is transformed into its corresponding
Unicode character.
" Any formatting-characters are removed.

The C# Language Specification Version 3.0 can be downloaded here:
http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c6
69b09/CSharp%20Language%20Specification.doc

Best regards,

Jie Wang ([email protected], remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Axel,

In addition to Stanimir's second point, here I quote the C# 3.0
specification 2.4.2 in case you and other community members who are
interested:

The prefix "@" enables the use of keywords as identifiers, which is useful
when interfacing with other programming languages. The character @ is not
actually part of the identifier, so the identifier might be seen in other
languages as a normal identifier, without the prefix. An identifier with an
@ prefix is called a verbatim identifier. Use of the @ prefix for
identifiers that are not keywords is permitted, but strongly discouraged as
a matter of style.

The example:
class @class
{
public static void @static(bool @bool) {
if (@bool)
System.Console.WriteLine("true");
else
System.Console.WriteLine("false");
}
}
class Class1
{
static void M() {
cl\u0061ss.st\u0061tic(true);
}
}
defines a class named "class" with a static method named "static" that
takes a parameter named "bool". Note that since Unicode escapes are not
permitted in keywords, the token "cl\u0061ss" is an identifier, and is the
same identifier as "@class".

Two identifiers are considered the same if they are identical after the
following transformations are applied, in order:
" The prefix "@", if used, is removed.
" Each unicode-escape-sequence is transformed into its corresponding
Unicode character.
" Any formatting-characters are removed.

The C# Language Specification Version 3.0 can be downloaded here:
http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c6
69b09/CSharp%20Language%20Specification.doc

Best regards,

Jie Wang ([email protected], remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top