Normally you would not want to use a reserved word as an identifier. But sometimes it can be very convenient, for instance for a code generator that wraps remoting calls or does ORM.
Both Delphi and C# have an escape for this:
- & prefix for Delphi.
- @ prefix for C#.
- [] embedding for VB.NET.
- “” embedding in many SQL syntaxes.
The prefixes are to tell the compiler knows you really know what you are doing, and are using a reserved word as an identifier.
The cool thing: in the Run Time Type Information (Delphi) or Reflection (C# and VB.NET) you will see the names without the prefix.
Some examples from StackOverflow:
& prefix for Delphi
private &string: Integer;
Kenny Cason answered delphi – Is it possible to use reserved words for field names? – Stack Overflow.
@ prefix for C#
private long @string;
Jon Skeet answered Use the long reserved word as a variable name in C# – Stack Overflow.
[] embedding for VB.NET
Private [string] As Integer;
Tobias Wittenburg asked vb.net – Reserved Keyword in Enumeration in C# – Stack Overflow.
The VB.NET way also works in Microsoft SQL Server and Microsoft Access.
“” embedding in many SQL syntaxes
Examples:
- escaping – How do I escape reserved words used as column names? MySQL/Create Table – Stack Overflow.
- How do I escape a reserved word in oracle – Stack Overflow.
- sql – Using Reserved Word TimeStamp as a field name (Firebird 2.5) – Stack Overflow.
Exception: Microsoft SQL Server and Microsoft Access use [] embedding.
- Creating table names that are reserved words/keywords in MS SQL Server – Stack Overflow.
- ms access – [] brackets in sql statements – Stack Overflow.
Note:
Some SQL implementations uses the embedding not only to allow for reserved words, but also to allow embedding spaces in identifiers.
Delphi, C# and VB.NET cannot do that, so be aware of that when performing ORM work.
–jeroen
Filed under: .NET, .NET 1.x, .NET 2.0, .NET 3.0, .NET 3.5, .NET 4.0, .NET 4.5, C#, C# 1.0, C# 2.0, C# 3.0, C# 4.0, C# 5.0, Delphi, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 8, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Delphi XE6, Delphi XE7, Development, Software Development, VB.NET, VB.NET 10.0, VB.NET 11.0, VB.NET 7.0, VB.NET 7.1, VB.NET 8.0, VB.NET 9.0
