I suggest you ...

Add support for constants in C# (and VB.net etc.) interfaces

3 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    birbilis shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    1 comment

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...
      • birbilis commented  ·   ·  Flag as inappropriate

        It is rather unfortunate that C# doesn’t support constants in interfaces, whereas Java does.

        If IL (intermediate language) that C# usually compiles to (when not using AOT that is like in .NET Native or Xamarin iOS) doesn’t support this, then the compiler could create a special sealed class to carry the constants.

        Then, wherever they’re used via the interface it could get them via that class. This would happen internally without the programmer noticing. The programmer should be able to access the constants using:

        ISomeInterface.SomeConstant

        SomeClass.SomeConstant, where SomeClass implements ISomeInterface

        just SomeConstant when the code is inside the body of SomeClass that implements the ISomeInterface

        just SomeConstant when there is a “using static” statement (that newer C# has introduced) like “using static ISomeInteface” or “using static ISomeClass”

        see a screenshot at the above URL, like in Java I'd like to use an interface instead of a separate static class to provide constants related to the interface

      Feedback and Knowledge Base