Software Engineering : This category relates to interesting computer related stuff that I am researching or reading about. Most of it is in the area of .NET technologies which is the focus of most of my computer related time at the moment.
Updated: 9/21/2004; 3:35:06 PM.

 








Subscribe to "Software Engineering" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

Subscribe To
Mark's Weblog

 
 

Sunday, July 27, 2003

Using @ to Disambiguate Keywords from Identifiers in C#
Google Search It

While reading the C# Language Specification today I noticed the use of the @ sign to disambiguate a keyword from an identifier.  When you do this the identifier is called a verbatim identifier.  So, for example, you could write code as follows:

class @class
{
   
static void Main()
    {
        @static(
false);
    }

    public static void @static(bool @bool)
    {
        if (@bool)
            System.
Console.WriteLine("true");
       
else
         
System.Console.WriteLine("false");
    }
}

Why would you want to do this you ask?  Well, what happens if you use an assembly that has a C# keyword for a public name because that keyword did not happen to be a keyword in the original language of the assembly.   That would prevent you from calling this assembly perhaps unless there was a way to disambiguate. 

Another place I would be very tempted is in the name of the variable that is returned from a function.  Often it is difficult to come up with a variable name.  "return" would be great but it is a keyword so you have to resort to "ret," which is an abbreviation or perhaps result, which just isn't quite the same.  Now I can just use @return. 

public string GetName()
{
   
string @return;
   
Console.Write("Enter your name:");
   
do
   
{
        @return =
Console.ReadLine();
    }
   
while (@return.Length == 0);
    return @return;
}

Cool!  How 'bout making this a coding standard? 

I am confident this idea will get lambasted but I still like it... so there!


11:46:20 PM   []    comment []

© Copyright 2004 Mark Michaelis.



 


July 2003
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Jun   Aug


Recent Posts