Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Brian Bondy
Hub FS
Full List

C# Keyword Identifiers

This falls into the "learn something new everyday but not necessarily entirely useful bucket".  An app I spend a bit of time on leverages the CodeDom heavily to spit out managed code.  While running through some test cases the other day I noticed that it was prefixing many identifier names with the @ symbol when the generated code was C#. 

At first I assumed this was a bug in my code but I noticed that the generated code compiled just fine.  A bit of digging through the C# spec turned up the purpose of the @ character. This allows you to use C# keywords as identifiers.  For instance you can have "class @class". 

http://msdn2.microsoft.com/en-us/library/aa664670(VS.71).aspx

VB has the same type of feature but they require the name to be wrapped in []. 

This still didn't answer the question of why my code generated with this character. It turns out this is a feature of the C# CodeDom.  When outputting an identifier, the C# CodeDom will prefix the identifier with @ if one of the following conditions are true.

  1. The identifier is a keyword
  2. The identifier is prefixed with two underscores.

It turns out the second rule exists to allow flexibility in the C# compiler implementation.  All identifiers that are prefixed with two underscores are inherently reserved for the implementation to provide such actions as extended keywords (reference at bottom of above link).

Published Wednesday, September 12, 2007 12:31 PM by Jared Parsons

Filed under: , ,

Comments

# MSDN Blog Postings » C# Keyword Identifiers @ Wednesday, September 12, 2007 1:01 PM

PingBack from http://msdnrss.thecoderblogs.com/2007/09/12/c-keyword-identifiers/

MSDN Blog Postings » C# Keyword Identifiers

New Comments to this post are disabled
Page view tracker