Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of @ Before Method Call [duplicate]

Tags:

c#

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?

like image 947
mahdi mahzouni Avatar asked Feb 17 '19 13:02

mahdi mahzouni


1 Answers

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.

like image 78
mjwills Avatar answered Nov 03 '22 00:11

mjwills