Allow the partial
modifier on properties to separate declaration and implementation parts, similar to partial methods.
// UserCode.cs public partial class ViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChanged] public partial string UserName { get; set; } } // Generated.cs public partial class ViewModel { private string __generated_userName; public partial string UserName { get => __generated_userName; set { if (value != __generated_userName) { __generated_userName = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(UserName))); } } } }Motivation
When we did extended partial methods, we indicated we would like to consider adding support for other kinds of partial members in the future. The community has shown enthusiasm for partial properties in particular.
.NET has a number of scenarios where a property implementation is some kind of boilerplate. One of the most prominent cases is INotifyPropertyChanged
, as seen above. Another is dependency properties. There are currently production source generators designed to handle these scenarios. These currently work by having the user write a field and having the generator add the corresponding property.
// UserCode.cs public partial class ViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChanged] private string _userName; } // Generated.cs public partial class ViewModel { public string UserName { get => /* ... */; set { // ... } } }
Under this scheme, users have to become familiar with the conventions for how the generator creates properties based on their fields. Additional workarounds are needed for users to be able to change accessibility, virtual-ness, attributes, or other aspects of the generated property. Also, using features like find-all-references requires navigating to the generated property, instead of being able to just look at the declarations in user code. All of these issues are solved fairly naturally by adding partial properties to the language.
Detailed designDetailed design has been moved to partial-properties.md.
Design meetingshttps://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-08-31.md#partial-properties
https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-11-02.md#partial-properties
alrz, canton7, FaustVX, mark-monteiro, kyoyama-kazusa and 160 morealrz, canton7, FaustVX, mark-monteiro, ufcpp and 30 morecanton7, Sergio0694, FaustVX, mark-monteiro, kyoyama-kazusa and 43 morehaltcase, AathifMahir, JohnTraDolta, KUNGERMOoN, fydar and 2 morepaulomorgado, AathifMahir, JohnTraDolta, KUNGERMOoN, fydar and 1 more
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4