C# Field Naming Guidelines?

后端 未结 17 1274
天命终不由人
天命终不由人 2020-12-02 16:37

I am going to be working on a bit of C# code on my own but I want to make sure that I follow the most widely accepted naming conventions in case I want to bring on other dev

相关标签:
17条回答
  • 2020-12-02 17:21

    The convention I use to distinguish between private class variables and method parameters is:

    private string baseName;
    private string prefixName;
    private string suffixName;
    
    public GameItem(string baseName, string prefixName, string suffixName)
    {
        this.baseName = baseName;
        this.prefixName = prefixName;
        this.suffixName = suffixName;
    }
    
    0 讨论(0)
  • 2020-12-02 17:21

    I too had doubts about this and then I decided check github codes of Microsoft. All most every source code I've looked at had underscore usage for private fields.

    https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/ document does not seem to mention about this usage.

    0 讨论(0)
  • 2020-12-02 17:27

    Generally there are two widely used ways to name fields (always using camelCase):

    Using an underscore prefix

    void F(String someValue) {
      _someValue = someValue;
    }
    

    Using this. to access the field and avoid name conflicts

    void F(String someValue) {
      this.someValue = someValue;
    }
    

    Personally I prefer the later, but I will use whatever convention is set forth by the organization I work for.

    0 讨论(0)
  • 2020-12-02 17:27

    Following Microsoft's naming conventions, private fields should be prefixed with an underscore.

    For example:

    private int _myValue;
    

    Good luck!

    0 讨论(0)
  • I've done much more with VB than C#, so I guess I carry over some practices (prejudices?) from the former to the latter.

    I like the private fields of properties to have a leading underscore - especially in C# due to the case-sensitivity (whose idea was that anyway?) And I prefix module-/class-wide variables with "m" as well to reinforce their scope.

    If you don't like that, you're really not gonna like this: I generally use type prefixes as well (except for property fields) - "o" for Object, "s" for String, "i" for Integer, etc.

    I can't really defend this with a peer-reviewed paper or anything but it works for us and means we're not tripped up by casing or field/parameter confusion.

    So ...

    Class MyClass
    
        Private msClassVariable  As String = ""
    
        Private _classProperty As Integer = 0
        Property Readonly ClassProperty() As Integer
            Get
                Return _classProperty
            End Get
        End Property
    
        Sub New()
    
            Dim bLocalVariable As Boolean = False
            if _classProperty < 0 Then _classProperty = 0
            msClassVariable  = _classProperty.ToString()
            bLocalVariable = _classProperty > 0
        End Sub
    
    End Class
    
    0 讨论(0)
  • 2020-12-02 17:29

    _camelCase for fields is common from what I've seen (it's what we use at our place and Microsoft prefer for the .NET Runtime).

    My personal justification for using this standard is that is is easier to type _ to identify a private field than this.

    For example:

    void Foo(String a, String b)
    {
        _a = a;
        _b = b;
    }
    

    Versus

    void Foo(String a, String b)
    {
        this.a = a;
        this.b = b;
    }
    

    I find the first much easier to type and it prevents me from ever accidentally assigning to the parameter called a instead of this.a. This is reinforced by a Code Analysis Maintainability Rule that states:

    • CA1500 Variable names should not match field names.

    My other reason, is that this. is optional (Visual Studio / Code prompts you to remove them) if it doesn't collide with a local variable or parameter name, making knowing which variable you are using harder. If you have an _ at the start of all private fields, then you always know which is a field and which is has local scope.

    0 讨论(0)
提交回复
热议问题