Delphi, C#, VB.NET and SQL all have escapes to use reserved words as identifiers
Posted by jpluimers on 2014/11/04
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






Leave a comment