user defined implicit conversion inconsistent with C# spec 1.2, compilerbug?

X

Xiaotian Guo

Hi,
I got compilation error while compiling following code, but according
to C# language spec 1.2 (section 6.4.3) , the code should compile. what
makes it more interesting is that, if I swap the order these 2 operators
appear in class SB, the code will compile without a problem.

Is this a bug in C# compiler? I am using

Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
for Microsoft (R) .NET Framework version 1.1.4322
Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.

Thanks!


//----------- code starts
using System;

class SB
{
public static implicit operator TD(SB src)
{
Console.WriteLine("TD");
return new TD();
}

public static implicit operator T(SB src)
{
Console.WriteLine("T");
return new T();
}
}

class S: SB
{
}

class T
{
}

class TD : T
{
}

public class MyClass
{
static void Main()
{
S s = new S();
T t = s;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top