Last active
September 5, 2025 19:55
-
-
Save karenpayneoregon/ac05a77dd8fdb3c0f496e5dab732bca6 to your computer and use it in GitHub Desktop.
C# partial properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "help": "https://go.microsoft.com/fwlink/?linkid=866610", | |
| "root": true, | |
| "dependentFileProviders": { | |
| "add": { | |
| "fileToFile": { | |
| "add": { | |
| "Client.Sets.cs": [ | |
| "Client.cs" | |
| ] | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #nullable disable | |
| using System.ComponentModel; | |
| namespace TODO.Models; | |
| public partial class Client : INotifyPropertyChanged | |
| { | |
| public int Id { get; set; } | |
| public partial string FirstName { get; set; } | |
| public partial string LastName { get; set; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #nullable disable | |
| using System.ComponentModel; | |
| using System.Runtime.CompilerServices; | |
| namespace TODO.Models; | |
| public partial class Client | |
| { | |
| public partial string FirstName | |
| { | |
| get; set => SetField(ref field, value); | |
| } | |
| public partial string LastName | |
| { | |
| get; set => SetField(ref field, value); | |
| } | |
| public event PropertyChangedEventHandler PropertyChanged; | |
| protected void OnPropertyChanged(string propertyName) | |
| => PropertyChanged?.Invoke(this, new(propertyName)); | |
| /// <summary> | |
| /// Sets the field to the specified value and raises the <see cref="PropertyChanged"/> event if the value has changed. | |
| /// </summary> | |
| /// <typeparam name="T">The type of the field.</typeparam> | |
| /// <param name="field">The field to set.</param> | |
| /// <param name="value">The value to set the field to.</param> | |
| /// <param name="propertyName">The name of the property. This is optional and will be automatically provided by the compiler.</param> | |
| /// <returns><c>true</c> if the field was changed; otherwise, <c>false</c>.</returns> | |
| protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = "") | |
| { | |
| if (EqualityComparer<T>.Default.Equals(field, value)) return false; | |
| field = value; | |
| OnPropertyChanged(propertyName); | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment