Question

We have some C# code where there are protected variables that have been named with underscores

protected string _name;
public string Name
{
   get
   {
      return _name;
   }
   set
   {
      _name = value;
   }
}

This generates CLS compliance warnings, as CLS does not like underscores at the beginning.

What are the implications of marking the protected variable with [CLSCompliant(false)]? I don't even know what languages this is an issue with to test things. If _name is simply inaccessible this is fine for our purposes, but if it causes naming ambiguity it is not.

Was it helpful?

Solution

Some languages don't support variables that start with an underscore. If a client using one of those languages wants to inherit from your class, he won't be able to access the _name field. What languages those are, I don't know.

From a design perspective, I'm wondering why you'd have a protected backing field when the public (and not virtual) property allows get and set directly. What's the point? In this case, giving inheritors access to the backing field provides no benefit and makes it impossible to change the implementation of the Name property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top