Some languages let you associate a constant with an interface:
The W3C abstract interfaces do the same, for example:
// Introduced in DOM Level 2:
interface CSSValue {
// UnitTypes
const unsigned short CSS_INHERIT = 0;
const unsigned short CSS_PRIMITIVE_VALUE = 1;
const unsigned short CSS_VALUE_LIST = 2;
const unsigned short CSS_CUSTOM = 3;
attribute DOMString cssText;
attribute unsigned short cssValueType;
};
I want to define this interface such that it can be called from C#.
Apparently C# cannot define a constant associated with an interface.
To answer your third question:
Although C# cannot, is there another .NET language which can define constants associated with an interface?
C++/CLI allows you to define literal
values in an interface, which are equivalent to static const
values in C#.
public interface class ICSSValue
{
public:
literal short CSS_INHERIT = 0;
literal short CSS_PRIMITIVE_VALUE = 1;
literal short CSS_VALUE_LIST = 2;
literal short CSS_CSS_CUSTOM = 3;
property DOMString^ cssText;
property ushort cssValueType;
}
You could then access the values via C#:
public static void Main()
{
short primitiveValue = ICSSValue.CSS_PRIMITIVE_VALUE;
Debug.Assert(primitiveValue == 1);
}
See this page on MSDN for more details.
Disclaimer: The design decision to disallow constant values in interfaces was a good one. An interface which exposes implementation details is most likely a leaky abstraction. In this example CSS Value Type is probably better off being an enumeration.