Search code examples
c#cls-compliant

What C# naming scheme can be used for Property & Member that is CLS compliant?


Consider the following code that is not CLS Compliant (differs only in case):

protected String username;

public String Username { get { return username;} set { username = value; } }

So i changed it to:

protected String _username;

public String Username { get { return _username;} set { _username = value; } }

Which is also not CLS-compliant (has leading underscore).

Is there any common naming scheme for members/properties that doesn't violate cls compliance


Solution

  • Rather than exposing the backing field, make your property virtual so inheritors can override the functionality without exposing the implementation of your backing field.

    private String username;
    
    public virtual String Username { get { return username;} set { username = value; } }
    

    Inherits shouldn't have to know anything your classes implementation.

    See Best way to expose protected fields