Records with positional parameters cannot be overloaded with a base type #46814
-
Version Used: e92e22c Steps to Reproduce: record A{record B{} record C(int B):A{public B X() => new B();}}
//record A{record B{} record C:A{new int B;public B X() => new B();}}
Expected Behavior: Since type names can be overloaded with property/field names, both versions compile. Actual Behavior: Only the bottom line compiles. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Records generally disallow implicitly shadowing base members, including types. The following scenario is also disallowed for this reason: record X1(int A);
// error CS8866: Record member 'X1.A' must be a readable instance property of type 'string' to match positional parameter 'A'.
record X2(string A) : X1(A.Length); Perhaps if some really compelling scenario came up, the restriction could be eased in your specific scenario, as there isn't really a compelling reason I can think of to access type members or static members via a derived type. e.g. why would you reference the type If we decided that this is something we would eventually want to do, then it might make sense to preemptively disallow accessing static or type members from the base type from a record. However it's not clear that we have the motivation or the time left in the .NET 5 schedule to do this. |
Beta Was this translation helpful? Give feedback.
Records generally disallow implicitly shadowing base members, including types. The following scenario is also disallowed for this reason:
Perhaps if some really compelling scenario came up, the restriction could be eased in your specific scenario, as there isn't really a compelling reason I can think of to access type members or static members via a derived type. e.g. why would you reference the type
C.B
in your program when you could be more clear by sayingA.B
?If we decided that this is something we would ev…