Difference between "private instance fields" and "private fields"

Reza Jaferi 331 Reputation points
2023-12-05T16:44:14.7166667+00:00

According to this Microsoft document, specific conventions for naming have been written, and the meaning of the following two items is not clear to me.

1: Use camelCase for method arguments, local variables, and private fields.

2: Private instance fields start with an underscore (_).

First, what are the differences between private fields and private instance fields, according to the contents?

Second, how do we use private instance fields whose names must begin with an underscore as function arguments according to the above two conventions (please provide an example)?

Third, does the following code follow Microsoft's C# identifier naming conventions?

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    #region fields
    //Private fields
    private float vehicleSpeed = 5.0f;
    private float turnSpeed = 25f;
    private float horizontalInput;
    private float forwardInput;
    #endregion

    #region functions/methods
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        forwardInput = Input.GetAxis("Vertical");
        //We'll move the vehicle forward
        transform.Translate(Vector3.forward * Time.deltaTime * vehicleSpeed * forwardInput);
        //We turn the vehicle 
        transform.Rotate(Vector3.up * Time.deltaTime * turnSpeed * horizontalInput);
    }
    #endregion
}
Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-12-06T05:33:30.65+00:00

    Hi @Reza Jaferi , Welcome to Microsoft Q&A,

    In C#, the terms 'private fields' and 'private instance fields' are usually equivalent, because in C# the field defaults to an instance field. Therefore, the term "instance" can be omitted here. In general, a private field is a field that can only be accessed in an instance (object) of a class.

    And these two points do not seem to conflict, they can be held at the same time.

    private int _myPrivateField;
    
    private void MyMethod(int myArgument)
    {
        // Do something with myArgument and _myPrivateField
    }
    
    

    CamelCase for method arguments, local variables, and private fields: The code appears to follow this convention. For example, vehicleSpeed, turnSpeed, horizontalInput, and forwardInput all use camelCase.

    You'll often find that in code examples, properties use "_" a lot:

    private string _name = "John Doe";
    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    

    Based on the provided code snippet, it seems to follow Microsoft's C# identifier naming conventions. The organization of regions (#region) is used, and the comments provide clarity on the purpose of fields and methods.

    However, coding style can vary among developers and teams, so it's essential to be consistent with the conventions used in a specific codebase or project.

    "Private instance fields are used when defining classes in object-oriented programming. They are not directly accessible outside the class and are meant for encapsulating data within the class. The names of private instance fields often begin with an underscore (_) to indicate their visibility scope and to signal to other developers that they should not be accessed directly from outside the class."

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-12-06T16:11:41.54+00:00

    The doc say to use camel case for private fields. In addition private instance fields (non static class private field) should start with an underscore, but should still be camel case. So in your sample code to follow the style the private float fields should start with an underscore

        #region fields
        //Private fields
        private float _vehicleSpeed = 5.0f;
        private float _turnSpeed = 25f;
        private float _horizontalInput;
        private float _forwardInput;
        #endregion
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.