7

Considering the Method Declaration Like this :

public void MethodName(string param){
       // method body
}

And then Calling it like this :

obj.@MethodName("some string");

What is the effect of @ before the method name ? Does it behave like putting it before a string which contains escaped characters?

mahdi mahzouni
  • 474
  • 5
  • 15

1 Answers1

14

This is used to allow you to use reserved words as identifiers such as class names, delegates and methods. For example: long:

namespace ConsoleApp4
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            @long();
        }

        public static void @long ()
        {
            // Some logic here
        }
    }
}

Remove the @ and it won't compile. It isn't often used.

Note that the code will compile fine if you change both long to long2 and remove the second @. So the use of @ means you can call a method name that is a reserved word - but it will also work if the method name is not a reserved word.

Note that the @ prefix does not form part of the identifier itself. So @myVariable is the same as myVariable.

Zack ISSOIR
  • 964
  • 11
  • 24
mjwills
  • 23,389
  • 6
  • 40
  • 63