I have the following VB.net interface that I need to port to C#. C# does not allow enumerations in interfaces. How can I port this without changing code that uses this interface?
Public Interface MyInterface Enum MyEnum Yes = 0 No = 1 Maybe = 2 End Enum ReadOnly Property Number() As MyEnum End Interface
In short, you can’t change that interface without breaking code, because C# can’t nest types in interfaces. When you implement the VB.NET versions’s interface, you are specifying that Number will return a type of MyInterface.MyEnum:
However, since C# can’t nest types inside interfaces, if you break the enumerator out of the interface, you will be returning a different data type: in this case, MyEnum.
Think about it using the fully qualified type name. In the VB.NET interface, you have a return type of
MyProject.MyInterface.MyEnum
In the C# interface, you have:
MyProject.MyEnum.
Unfortunately, code that implements the VB.NET interface would have to be changed to support the fact that the type returned by MyInterface.Number has changed.
IL supports nesting types inside interfaces, so it’s a mystery why C# doesn’t:
{ .property instance valuetype TestInterfaces.MyInterface/MyEnum Number { .get instance valuetype TestInterfaces.MyInterface/MyEnum TestInterfaces.MyInterface::get_Number() }
{ .field public static literal valuetype TestInterfaces.MyInterface/MyEnum Maybe = int32(2)
}
If you have lots of code in other assemblies that make use of this interface, your best bet is to keep it inside a separate VB.NET assembly, and reference it from your C# projects. Otherwise, it’s safe to convert it, but you’ll have to change any code that uses it to return the different type.