0

My target framework is .NET 5 and type of application is Console. I am using Visual Studio 2019 version 16.10.1 as an IDE. I wanted to see if I indeed get a warning if I use CLSCompliant hint in my C# code and purposely violate CLS compliance. The violation is achieved by referring to a variable of type UInt32 since is not specified in the CLS. But it appears I am not getting any error or warning whatsoever.

Here is the code:-

using System;

[assembly: CLSCompliant(true)]
namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            UInt32 x = 12;
            Console.WriteLine(Convert.ToString(x));
        }
    }
}

Do I have to change some IDE settings for this to show me CLS related warnings?

Dhiraj
  • 3,396
  • 4
  • 41
  • 80
  • 1
    CLS-Complience affect only public API, you can create internal non-complient types and it's allowed – JL0PD Jul 04 '21 at 04:58

1 Answers1

1

CLS-complience is a contract of your library meaning it can be used from any language. For example, some languages doesn't support unsigned integers or case insensitive

It means that you can write your internal API or implementation any way you like, but have to follow rules for public API

[CLSCompliant(true)]
public class MyClass
{
    public void Yes(int value) {}
    public void No(uint value) {} // warning: uint is not compliant
    public void no(int value) {} // warning: No & no is same word
    public void Yes2(int value)
    {
        Span<byte> bytes = stackalloc byte[10]; // we can do weird things inside, but as long public API is compliant, it won't cause warnings
        Helper.ProcessBytes(bytes);
    }
}

You can read more in docs

JL0PD
  • 3,698
  • 2
  • 15
  • 23