Implicit array casting in C#

Posted by Malki on Stack Overflow See other posts from Stack Overflow or by Malki
Published on 2010-06-15T18:00:07Z Indexed on 2010/06/15 18:02 UTC
Read the original article Hit count: 239

Hi,

I have the following classes with an implicit cast operator defined:

class A
{
    ...
}
class B
{
    private A m_a;

    public B(A a)
    {
        this.m_a = a;
    }

    public static implicit operator B(A a)
    {
        return new B(a);
    }
}

Now, I can implicitly cast A to B.

But why can't I implicitly cast A[] to B[] ?

static void Main(string[] args)
{
    // compiles
    A a = new A();
    B b = a;

    // doesn't compile
    A[] arrA = new A[] {new A(), new A()};
    B[] arrB = arrA;
}

Thanks, Malki.

© Stack Overflow or respective owner

Related posts about c#

Related posts about array