Why do I get, "Identifier 'Logic.DomainObjectBase._isNew' is not CLS-compliant"?
public abstract class DomainObjectBase
{
protected bool _isNew;
}
Why do I get, "Identifier 'Logic.DomainObjectBase._isNew' is not CLS-compliant"?
public abstract class DomainObjectBase
{
protected bool _isNew;
}
The leading underscore concomitant with _isNew
being visible (i.e., not private).
From the Common Language Specification:
CLS-compliant language compilers must follow the rules of Annex 7 of Technical Report 15 of the Unicode Standard 3.0, which governs the set of characters that can start and be included in identifiers. This standard is available from the Web site of the Unicode Consortium.
If you look this up:
That is, the first character of an identifier can be an uppercase letter, lowercase letter, titlecase letter, modifier letter, other letter, or letter number. The subsequent characters of an identifier can be any of those, plus non-spacing marks, spacing combining marks, decimal numbers, connector punctuations, and formatting codes (such as right-left-mark). Normally the formatting codes should be filtered out before storing or comparing identifiers.
Basically, you can't start an identifier with an underscore - this violates CLS compliant on a visible (public/protected) field.
The underscore causes the problem. Common practice is that the underscore is reserved for private fields. protected / public members should be properly cased and named.
For example:
public abstract class DomainObjectBase{
private bool _isNew;
protected bool IsNew { get { return _isNew; } set { _isNew = value;} }
}
OR, if you want to use 3.x and get rid of the private field:
public abstract class DomainObjectBase{
protected bool IsNew { get; set; }
}
The leading _ is non-CLS compliant
Microsoft StyleCop will analyze your code, and provide links to the relevent documents explaining why it is not CLS compliant.
CLS Compliance is to do with interoperability between the different .Net languages. The property is not CLS compliant because it starts with an underscore and is public (Note: protected properties in a public class can be accessed from outside the assembly). Although this will work if the property is accessed from C# it may not if it is accessed from other .Net languages that don't allow underscores at the start of property names, hence it is not CLS-Compliant.
You are getting this compiler error because somewhere in your code you have labelled your assembly as CLS compliant with a line something like this:
[assembly: CLSCompliant(true)]
Visual Studio includes this line in the AssemblyInfo.cs file which can be found under Properties in most projects.
To get around this error you can either:
A. Rename your property (recommended).
protected bool isNew;
B. Set your whole assembly to be non CLS compliant.
[assembly: CLSCompliant(false)]
C. Add an attribute just to your property.
[CLSCompliant(false)]
protected bool _isNew;
D. Change the scope of the property so that it can not be seen outside the assembly.
private bool _isNew;