Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 996731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:55:34+00:00 2026-05-16T06:55:34+00:00

I was browsing some documentation for a physics library for XNA and noticed an

  • 0

I was browsing some documentation for a physics library for XNA and noticed an example someone had used for creating a class for a Car.

This is a pretty simple example:

Class Car
{
   private float gravity;
   private float maxSpeed;

   public Car(float gravity, float maxSpeed)
   {
        this.gravity = gravity;
        this.maxSpeed = maxSpeed;
   }
}

Now when i’ve created a constructor and set up the assigning of the passed in parameters I would do it this way:

Class Car
{
  private float _gravity;
  private float _maxSpeed;

  public Car(float gravity, float maxSpeed)
  {
       _gravity = gravity;
       _maxSpeed = maxSpeed;
  }
}

Is there any advantage to either approach? I’ve only come across this a few times but I assume there’s a good reason for doing so, I’m just looking for the “best practice” way so to speak.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:55:34+00:00Added an answer on May 16, 2026 at 6:55 am

    Both examples are functionally identical, and both methods are used by different people. It’s up to you which naming convention you go with. If you do give the field the same name as the parameter, then you will have to use this to access it.

    The important thing is to be consistent in your naming. I personally prefer to access fields with the this keyword, but there are definitely a lot of people around here that like the underscore approach.

    Underscore:

    If you prefix your private fields with an underscore, then you will always need to use that wherever you refer to it. It is clear (provided that you know the convention) that it is a private field, just from looking at it. Unfortunately, it is also very ugly, but that’s just my opinion.

    No underscore:

    If you do not prefix your private fields with an underscore, then you have the option of referring to it directly or via the this keyword. This means that it possible to forget, and in your example you might assign the argument to itself: gravity = gravity; This should at least generate a warning, though. If you use the StyleCop tool, then it will by default enforce that you always access private fields through the this keyword.

    See these similar questions for more answers:

    • To underscore or to not to underscore, that is the question
    • Underscore prefix on member variables. intellisense
    • C# member variables / fields naming convention
    • C# Field Naming Guidelines?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.