Sunday, May 1, 2011

Best practice for renaming property/method names that are reserved words?

I'm creating a car class. Make and model are properties but both make and model appear to be reserved words in C#. What's the best practice for naming properties/methods when your preferred name is a reserved word?

My first instinct is to call the properties CarMake, CarModel (so a convention of ClassNamePropertyName). Is there some better convention or is this the best approach?

EDIT>> My mistake, make and model aren't actually reserved words. VS intelliesense and code coloring made it appear so to me at first glance. Though my question does stand for future reference.

Thanks!

From stackoverflow
  • In VB.NET, you can enclose the property name within square brackets:

    Public Property [Make] As String
    

    (I'm not sure about C#, but you also tagged the post as vb.net)

  • Neither make nor model are reserved C# words. In the case that you do need to use a reserved word, the best thing to do is to try to come up with a synonym.

    Cory House : Visual Studio has intellisense for both make and model as though they're reserved words. Actually, it appears they're part of the BCL. Is that correct?
  • I usually try to think of some synonymous alternative word, or variation, though it doesn't sound like that is applicable for you.

    Alternatively, you can use the '@' prefix (in C#) or [square braces] (in VB.NET) to explicitly avoid the conflict with reserved words in the language.

0 comments:

Post a Comment