Is warning CS3006 valid in this case?

▼魔方 西西 提交于 2019-12-14 04:18:34

问题


The code below generates a warning CS3006 "Overloaded method MyNamespace.Sample.MyMethod(int[])' differing only in ref or out, or in array rank, is not CLS-compliant".

Is this warning valid, i.e. is this genuinely not CLS-compliant? I'd have thought an explicit interface implementation would not count as an overload.

[assembly: CLSCompliant(true)]
namespace MyNamespace
{

    public class Sample : ISample
    {
        public void MyMethod(int[] array)
        {
            return;
        }

        void ISample.MyMethod(ref int[] array)
        {
            this.MyMethod(array);
        }
    }

    public interface ISample
    {
        void MyMethod([In] ref int[] array);
    }
}

回答1:


CLS compliance only applies to the visible part of your class. Therefore, you'd think that the ref int[] is not public and therefore not relevant. But it is visible, through the interface.

The users of your code know that Sample provides void MyMethod(int[]). They also know that it implements ISample which provides void MyMethod(ref int[]). Therefore, I believe it is in fact not CLS-Compliant.


EDIT: Eric Lippert has commented on the original question that he believes this is in fact a compiler bug and that the original code is CLS-Compliant.


This, however, is valid:

[assembly: CLSCompliant(true)]
namespace MyNamespace
{
    public class Sample : ISample, ISample2
    {
        void ISample.MyMethod(ref int[] array)
        {
        }

        void ISample2.MyMethod(int[] array)
        {
        }
    }

    public interface ISample
    {
        void MyMethod(ref int[] array);
    }

    public interface ISample2
    {
        void MyMethod(int[] array);
    }
}

That is because CLS defines that two interface may define conflicting methods with the same name or signature and the compiler must know how to tell the difference - but again, only when the conflict is between two interfaces.



来源:https://stackoverflow.com/questions/897249/is-warning-cs3006-valid-in-this-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!