5

I have a .NET 3.5 C# project that has a namespace of SampleNamespace.Tools.Sample.

If I add an assembly called "Samplenamespace.Utils.Example" to my project I get the following warning:

Identifier 'Samplenamespace' differing only in case is not CLS-compliant

Note the lower case 'n' in Samplenamespace.

I'm not even using the reference assembly in my project at the moment. Simply adding it as a reference causes the warning.

Why is the compiler complaining about this considering I'm not even exposing any references to the assembly in my public classes?

Any workaround?

sth
  • 222,467
  • 53
  • 283
  • 367
user115909
  • 51
  • 1
  • 2

2 Answers2

7

Not all .NET languages are case sensitive (VB for example) when you have mixed namespaces like this, diffing only in case (to use the wording of the warning) your code may not be accessable to other developers.

That may not be your case, which is why it's a warning (which in my shop we treat as an error)

Ralph Shillington
  • 20,718
  • 23
  • 91
  • 154
  • Thanks Ralph, i'm still not understanding why this should matter since my project is not exposing anything form the referenced assembly to any potential VB clients. I'm effectively abstracting the VB client from the underlying assembly? – user115909 Jun 02 '09 at 11:13
  • Since VB is not case-sensitive, It is not able to differentiate between SampleNamespace.A and Samplenamespace.A and hence it is not able to determine which type to invoke. To prevent such ambiguity the C# Compiler flags this up so that you can only create types and members that can be invoked by everyone without any ambiguity. – Gishu Jun 02 '09 at 11:19
  • I think, It is a blind check-and-flag, it is not considering how you are actually using or exposing the types. – Gishu Jun 02 '09 at 11:20
  • 2
    There is no way it can *know* whether the code is accessed from VB. What if someone on another computer next week is going to write an assembly in VB which calls some functions in yours? Anything in your assembly is available to other assemblies. And those other assemblies may be written in any .NET language. – jalf Jun 02 '09 at 11:34
3

It is simply warning you since not all languages that can consume the types within your solution will be aware of the difference (and may be unable to use the types).

I think that you can avoid this warning by marking your assembly as being non-CLS compliant (in the AssemblyInfo.cs file) (read more here):

[assembly:CLSCompliant(false)]

Not sure I think it's a good idea though...

Update: I think that the reason that the warning is issued though nothing is publicly exposed is that namespaces do not have access modifiers. You could perhaps say that namespaces are always public, so they are exposed to potential clients, even though they may not contain any public types.

Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
  • Fredrik, i could make the code none CLS-Compliant but i'd rather not considering the any client code of my app is not exposed to this assembly. It just doesn't seem like a valid situation to break CLS-Compliance. – user115909 Jun 02 '09 at 11:19
  • I got curious as well and did some tests; see my update in the answer. – Fredrik Mörk Jun 02 '09 at 11:24
  • That makes since Fredrik. I suppose my only options are to either change the namespace or set CLS compliance to false. Thanks for you help – user115909 Jun 02 '09 at 11:47