locked
A field prefixed with an underscore? RRS feed

  • Question

  • Hi,

     

      I seem to run into code from books wherein the name of a field in either a struct or a class is prefixed with an underscore. What does this mean? The books don't explain them. I knew for one thing that it couldn't have been naming convention, because they always advise NEVER EVER use underscores when it comes to naming fields, methods, properties, etc. Can anyone help me, please? Here's an example:

     

     class Order

     {

        private Int _orderID;

        private ArrayList _orderItems;

          public int OrderID;

            {

              get {return _orderID;}

              set {_orderID = value;}

            }

     

    ...
    Saturday, May 12, 2007 3:35 PM

Answers

  • Usually the underscore prefix means it is a class scope variable, I've seen it in different flavors for example: _FirstName, mFirstName, gFirstName and strFirstName.

    The key to naming conventions is to be consistent especially when working in groups so you can better understand the code. You may see alot of code for public variables written that  way,

    Partial Class Person

     

       Dim _FirstName As String

     

       Public Property FirstName() As String

          Get

              Return _FirstName

          End Get

         Set(ByVal value As String)

              _FirstName = value

         End Set

      End Property

     

    End Class

     

    Regards, Burl
    Saturday, May 12, 2007 7:45 PM

All replies

  • I am still new to C#, so I may be wrong here, but I don't think that this means anything.  The _ is part of the variable name, if it were part ot the type name, it might mean something.  For instance:

     

    public int Test;  is a public int32 called test

    public int[] Test; is a public int32 array called test

     

     

     

    Saturday, May 12, 2007 3:55 PM
  • I've tested this underscore-prefixing style, and the only reason that I could find why to use it is because it pops out as the first entry in the IntelliSense box. Perhaps because of the convenience it offers? I still don't think that's reason enough to break your own rules...
    Saturday, May 12, 2007 4:16 PM
  • Well, the intellisense is one reason, but what if you have a database field called orderID and you need to use a variable called orderID.  Since C# isn't case sensative, this can cause problems later in code (believe me).  You could use the _ to denote the vairables that coincide with object names that are created outside the application.

    Saturday, May 12, 2007 7:23 PM
  • Usually the underscore prefix means it is a class scope variable, I've seen it in different flavors for example: _FirstName, mFirstName, gFirstName and strFirstName.

    The key to naming conventions is to be consistent especially when working in groups so you can better understand the code. You may see alot of code for public variables written that  way,

    Partial Class Person

     

       Dim _FirstName As String

     

       Public Property FirstName() As String

          Get

              Return _FirstName

          End Get

         Set(ByVal value As String)

              _FirstName = value

         End Set

      End Property

     

    End Class

     

    Regards, Burl
    Saturday, May 12, 2007 7:45 PM
  • I see. Ok thanks Smile
    Sunday, May 13, 2007 2:27 AM