Hardcoding property names as strings breaks when you rename. Use nameof for refactor-safe code.
Bad:
public class Person
{
public string Name { get; set; }
public void UpdateName(string newName)
{
Name = newName;
OnPropertyChanged("Name"); // Breaks if you rename property!
}
}
Good:
OnPropertyChanged(nameof(Name)); // Compiler-verified!
If you rename Name property, compiler updates nameof() automatically.
Other Uses:
throw new ArgumentNullException(nameof(user));
logger.LogError($"{nameof(ProcessOrder)} failed");
