Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument type is not CLS-compliant, why?

Tags:

c#

I get the warning like this alt text If you know what it is, please explain & help me to get rid of it. Nam.

like image 419
Nam G VU Avatar asked Aug 13 '10 09:08

Nam G VU


People also ask

Which is not a CLS compliant language?

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).

What does CLS compliant mean?

Being CLS compliant means that you can write code that can be consumed by any language that can be compiled and run on the CLR. But CLS compliance is not required, giving you the flexibility in cases where CLS compliance would be hard or impossible to do.


2 Answers

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.

like image 102
Jarrett Meyer Avatar answered Sep 28 '22 14:09

Jarrett Meyer


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.

like image 37
whoisthemachine Avatar answered Sep 28 '22 15:09

whoisthemachine