Allow primary constructors to reference class/record/struct constants #7859
Unanswered
CodeSetting
asked this question in
Language Ideas
Replies: 2 comments 3 replies
-
namespace TestNamespace
{
public class TestClass(int value = TestClass.DefaultValue)
{
private const int DefaultValue = 123;
public int Value { get; } = value;
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
-
This is an interesting argument. However, I would propose that this ambiguity should be resolved in the same way as it is within a traditional constructor. In short, it is my opinion, that a primary constructor should have the same scoping rules as a traditional constructor. To me, it doesn't make sense that it has different rules. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I like the brevity of the new primary constructor syntax. However, I do notice a likely oversight. Currently, it is impossible to reference constants defined within the associated class/record/struct. This can lead to some repetitive (and unclear) code.
Consider the following:
In this instance, it would be better if the
DefaultValue
constant could be referenced in the primary constructor, as follows:I propose that all constants, that are defined in a class/record/struct, should also be accessible to its primary constructors.
Beta Was this translation helpful? Give feedback.
All reactions