Home > Net >  What does the @ (At sign) symbol do to a variable? i.e. @variable name?
What does the @ (At sign) symbol do to a variable? i.e. @variable name?

Time:12-06

I (sadly) came across some old c# .NET 4.8 code that I wrote and I noticed a typo. The code has been functioning fine in production for many years, but I was wondering if there is a difference between

static public DataTable GetReportIDRange(int BeginningID, int @EndingID)

and

static public DataTable GetReportIDRange(int BeginningID, int EndingID)

I removed the @ and it is still working fine.

CodePudding user response:

@ allows a reserved keyword to be used as a variable name


static DataTable GetReportIDRange(int BeginningID, int @static)
{
   return null;
}

but in your case, you have to check if "EndingID" is being declared as some specific type or keyword.

Normally it is always says to avoid using @ with parameters or variables but, we can have situations where third party API requesting to pass a parameter with reserved keywords, for example, lets say some API need an input and variable name has to be "event" , which is reserved keyword in C# in that case @ is helpful.

hope this clears the things in better way.

  • Related