[Proposal] Auto-implemented interface properties #7925
-
Imagine we have an interface: public interface IRectangle<TCoord>
where TCoord : unmanaged, INumber<TCoord>
{
TCoord X1 { get; set; }
TCoord Y1 { get; set; }
TCoord X2 { get; set; }
TCoord Y2 { get; set; }
} Then in every struct implementing the above interface we have to explicitly define those properties: public struct RectangleI32 : IRectangle<int>
{
public int X1 { get; set; }
public int Y1 { get; set; }
public int X2 { get; set; }
public int Y2 { get; set; }
} My proposal is to allow omitting implementation of such trivial auto-properties and just generate their backing fields behind the scenes: public struct RectangleI32 : IRectangle<int>
{
} In case if some of those properties need some logic or just to be non auto-property then they could be implemented explicitly as usual. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
I'm not a fan of mixing implicit implementation with explicit (e.g. some is done for me and some is not), it should be all implicit or all explicit in my honest opinion. In this situation, imagine if, instead of working with a public abstract class Rectangle<TNumber>
: IRectangle<TNumber>
where TNumber : unmanaged, INumber<TNumber>
{
TNumber X1 { get; set; }
TNumber Y1 { get; set; }
TNumber X2 { get; set; }
TNumber Y2 { get; set; }
}
public sealed class RectangleI32
: Rectangle<int>; I think instead of asking for partially implicit implementation of interfaces on structs, perhaps |
Beta Was this translation helpful? Give feedback.
-
Well, actually regular properties and auto-properties are also of that kind of |
Beta Was this translation helpful? Give feedback.
-
I think it's not possible as of today since there is no partial properties support atm. And even if it was possible I'd have to define them first in my struct. |
Beta Was this translation helpful? Give feedback.
-
Check it out, Visual Studio has an option to do this automatically: |
Beta Was this translation helpful? Give feedback.
You wouldn't need partial properties, the source generator could emit the full auto-properties corresponding to the unimplemented properties of the interface.