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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With