Argument type is not CLS-compliant, why?
Asked Answered
C

3

38

I get a warning:

Argument type is not CLS-compliant

What is it, and how can I get rid of it?

Caudill answered 13/8, 2010 at 9:52 Comment(1)
What does INotificationService look like?Silberman
T
59

In your AssemblyInfo.cs file, you've probably got a line that reads

[assembly:CLSCompliant(true)]

If you do, then the following rules must be met. (Copy-Pasta from http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/)

  1. Unsigned types should not be part of the public interface of the class. What this means is public fields should not have unsigned types like uint or ulong, public methods should not return unsigned types, parameters passed to public function should not have unsigned types. However unsigned types can be part of private members.

  2. Unsafe types like pointers should not be used with public members. However they can be used with private members.

  3. Class names and member names should not differ only based on their case. For example we cannot have two methods named MyMethod and MYMETHOD.

  4. Only properties and methods may be overloaded, Operators should not be overloaded.

Togoland answered 13/8, 2010 at 9:55 Comment(1)
Unsigned types may be part of a public interface, if they do not expose any functionality which code written in a language without unsigned types might want to use but be unable to do so. For example, a method to store a 32-bit unsigned value into four consecutive bytes could legitimately contain overloads for both UInt32 and Int64, and a class could provide a method to convert four consecutive bytes into UInt32 if it also provided one that would return the same value as an Int64.Swaggering
K
14

This is an old question, but I thought a better explanation was due for future investigators (such as myself).

First off, the links in the other answers provide great detail into the reason this warning is given.

However, to summarize, code written for the Common Language Runtime (such as C#) is CLS-Compliant if it can interface with other languages designed for the CLR. This means that certain datatypes specific to the language that are not common to the entire runtime are not compliant. The quick and easy fix for this in regards to variables and methods is to give them the visibility modifier internal which specifies that the method, class, property, etc. is not visible outside of the assembly it is being built for. This should only be done on those items which you do not need or want to be used outside of the assembly; for those that you want visible, use datatypes that are CLS-compliant.

Koziarz answered 9/10, 2014 at 17:54 Comment(0)
M
5

Some more info:

And you can get those warnings in a CLS-compliant assembly, that references a non-CLS-compliant assembly (and exposes its non-CLS-compliant members):

Mahla answered 4/5, 2011 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.