What C # naming scheme can be used for a CLS compatible property and member?

Consider the following code that does not comply with CLS (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 also does not correspond to CLS (has a leading underscore).

Is there a general member / property naming scheme that does not violate CLS compliance

+5
source share
4 answers

Instead of displaying a support field, make your property virtual so that heirs can override functionality without exposing the implementation of your support field.

private String username;

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

Inheritance should not know anything about the implementation of your classes.

See Best Way to Open Protected Fields

+5

Visual Basic, # :

Visual Basic :

(_).

, .

, .

1023 .

:

, (_), Common Language Specification (CLS), CLS- , . , CLS-.

MSDN.

Common Language Specification MSDN, , , CLS: 7 15 Unicode 3.0.

+2

MFC to the rescue. Just use the old m_ prefix:

private string m_Username;
public string Username ...
+1
source

What are you trying to achieve with this?

public String Username {get; protected set};

or

private String _username;

protected void setUserName(String argUsername);
{
  if (_username != Username) // an overload of String.Compare would be better here
  {
    _username = value;
    // Do the stuff you have to do because username has changed
  }
}

public String Username {get {return _username;} protected set {setUsername(value);}}

Your way to get CLS compliance, you will have to call you the pulky and prescribed versions of different names, which would be "confused."

0
source

All Articles