56

Why do I get the compiler warning

Identifier 'Logic.DomainObjectBase._isNew' is not CLS-compliant

for the following code?

public abstract class DomainObjectBase
{
    protected bool _isNew;
}
7
  • 25
    You probably should not be marking non-private members with an underscore anyway. I know that everyone has their own style, but others will almost certainly think that the field is private out of convention. Jul 28, 2009 at 17:25
  • @EdS. Which convention?
    – Pharap
    Jun 16, 2015 at 2:16
  • Seems to have been a VB convention at one time, it also appears to be out of style for C++, C#, more details than will fit in this box found here: stackoverflow.com/questions/3136594/… Jun 16, 2015 at 3:10
  • 1
    @Pharap: It's common to name a private member with a leading underscore. I believe that fieldName is more common in C# (at least, it's what I see more often than not), but some don't like it, myself included, because it forces you to write this. all over the place. Jun 16, 2015 at 17:10
  • 1
    @Pharap: Actually, they don't: msdn.microsoft.com/en-us/library/ms229012(v=vs.110).aspx. "Internal and private fields are not covered by guidelines". You're referring to static members, a different beast entirely. Also, there's nothing wrong with using a single leading underscore for private identifiers which are followed by a lowercase letter in C++. Anything in the global namespace or followed by a capital, yeah. Jun 17, 2015 at 17:30

8 Answers 8

80

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.

2
  • 1
    What is a "letter number"?
    – Kevin
    Mar 1, 2017 at 16:52
  • 2
    @Kevin: All of the terms in the snippet above refer to Unicode categories: "Letter, uppercase", "Letter, lowercase", "Letter, titlecase", "Letter, modifier", "Letter, other" and "Number, letter". Roman numerals are one example of a 'letter number'. See fileformat.info/info/unicode/category/Nl/list.htm
    – Joren
    Apr 27, 2017 at 13:21
43

CLS compliance has 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:

  1. Rename your property (recommended):

    protected bool isNew;
    
  2. Set your whole assembly to be non CLS compliant:

    [assembly: CLSCompliant(false)]
    
  3. Add an attribute just to your property:

    [CLSCompliant(false)]  
    protected bool _isNew;
    
  4. Change the scope of the property, so that it can not be seen outside the assembly.

    private bool _isNew;
    
8
  • So, when you have a public property with a protected variable, what's the best convention? Aug 15, 2011 at 14:46
  • 3
    Personally I like to make all fields private. If I need to increase the scope I would wrap it in a Property Get/Set. Jul 31, 2012 at 11:56
  • @MartinBrown: There are many situations where a class will have a public property whose setter calls a protected method which processes updates, but where derived types might have a legitimate need to use the field directly and post updates afterward (e.g. if a class derived from a control has a method to change both its color and caption, but the base doesn't, it may be helpful for the derived class to change both fields and then call the update method once). My inclination would be to use "Characteristic" and "_characteristic", since both VB.NET and C# accept it. What would you suggest?
    – supercat
    Jan 22, 2015 at 17:18
  • @supercat: In my 13 years programming full time with .Net I don't think I can recall ever having needed to do that. But if I did I would go for a camelCase field name with no underscore. Jan 22, 2015 at 23:07
  • @MartinBrown: A field name which matched the property name except for case would not only be CLS compliant, but it would render the identifiers unusable in VB.NET.
    – supercat
    Jan 22, 2015 at 23:18
28

The leading underscore concomitant with _isNew being visible (i.e., not private).

1
  • 12
    +1 But you need to include the fact that the member is non-private which, along with the leading underscore, makes the member's name not CLS-Compliant. Jul 28, 2009 at 15:57
7

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; }
}
0
2

A CLS-compliant identifier should not start with an underscore.

1

It's the underscore. See this article.

1

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.

2
  • 1
    I like the idea of StyleCop, but its rules conflict with FxCop rules, Resharper's reformatter and Visual Studio reformatter. Jul 28, 2009 at 16:22
  • StyleCop and FxCop are both produced by Microsoft (although by different product teams) however I believe that StyleCop is the later, and therefore more preferred one if you wish to use a "Microsoft" code style.
    – Frozenskys
    Jul 28, 2009 at 16:32
0

Because the name of the data member, _isNew, start's with an underscore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.