0

I was given a project of a some developer and during looking throuhg the code I found such code:

public interface IPackage
{
    void @Do();
} 

I've just seen usage of @ in context with strings. But what does that mean in such context? Can somebody explain please? Thank you in advance!

IDeveloper
  • 1,249
  • 2
  • 13
  • 22

2 Answers2

3

From MSDN:

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.

Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59
2

The @ character allows you to give your variables names that are reserved as keywords. A common and useful use case for this is:

public static void AnExtensionMethod(this SomeObject @this)
{
    @this.AMethod();
}

In your example the @ seems obsolete in C# as Do is not a keyword (do is). It is in VB.NET though, so if this interface will be consumed by VB.NET clients it is needed. In your case it is fine only if there really wasn't any better name for the method than Do. As it is now it seems not very readable. Conceptually "package.do" can mean anything so you can call it x as well. But maybe in your domain language this is a more precise term.

The documentation states:

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.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130