Search code examples
.netpropertieslanguage-design

Why do property setters and getters clash with get_X and set_X methods?


In .NET properties are supposed to be first class citizens however in the IL code property getters and setters are implemented as get_PropertyName and set_PropertyName.

class Property
{
    int Value { get { return 42; } }
    int get_Value() { return 6 * 9; }
    void set_Value(int i) { } // Error even though Value is a read only property
}

Output:

error CS0082: Type 'SO.Property' already reserves a member called 'get_Value' with the same parameter types

error CS0082: Type 'SO.Property' already reserves a member called 'set_Value' with the same parameter types

Why did the designers of .NET decide to use a name that may clash with user code? They could have used an illegal character (as Java uses $ for inner class stuff).


Solution

  • For languages that don't have the concept of properties, such as J# (which may very well have influenced that design decision), it's still necessary to have a simple name to call them.