Properties with optional {get;set/init;}? / Compile public fields as auto properties #4842
Replies: 5 comments 13 replies
-
If you don't use custom getters / setters - what is wrong with normal fields? |
Beta Was this translation helpful? Give feedback.
-
public record Person(
string Name,
int Age
); That's close enough? That gives you properties which are |
Beta Was this translation helpful? Give feedback.
-
At least for Rust (and FSharp) these aren't properties, they're still fields, and so are equivalent to just the bare I actually consider this a problem - at least some of what I want to do in Rust would have been nicer if I could use a different backing field than what is exposed. |
Beta Was this translation helpful? Give feedback.
-
Properties will always need a different syntax from fields given the team has expressed a desire to avoid dialects, so they will always be in some way more verbose. I'm of the opinion that the current auto-property syntax is not at all burdensome. It's only a couple of additional keystrokes if you type them by hand, and most IDEs will have a template/snippet which will "type" that for you. Those other languages chose to not have fields (or only have one syntax, whether they are fields or blur the lines between fields and properties). C# has both and will always have both. |
Beta Was this translation helpful? Give feedback.
-
public record Person {
string Name;
int Age;
} This is already valid syntax, and changing what that code means would be an unacceptable breaking change. So you would need to invent some new, more complicated syntax for this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Pretty much every other language allows defining properties in a simple syntax like:
F# records:
Rust:
Typescript:
Kotin makes properties first-class https://discuss.kotlinlang.org/t/property-vs-field/8179
In Kotlin backing field cannot be defined directly... you need to use
someProperty.field
I think it would be great if there was some solution in C# to make defining properties like
{get;set;}
or{get;init;}
in some easier way. Either some .csproj switch that will automatically convert fields to props or some other way.We will be getting primary constructors #2691 and
field
keyword.I think it would be great if we could define properties without explicitly adding get/set/init
e.g. for records it would automatically default to
{get;init;}
.. unless explicitly setBeta Was this translation helpful? Give feedback.
All reactions