public sealed class User(string name, int age) {
public void PrintNameAndAge() {
Console.WriteLine($"User name: {name}, Age: {age}");
}
public void PrintNameAllCapsIfOld(int oldThreshold) {
if (age >= oldThreshold) {
name = name.ToUpperInvariant(); // Oops- did we really mean to change this permanently for this User instance?
}
Console.WriteLine(name);
}
}
static void Test() {
var user = new User("Ben", 33);
user.PrintNameAllCapsIfOld(30); // "BEN"
user.PrintNameAndAge(); // "User name: BEN, Age: 33" -- we've accidentally altered the name field in the previous method invocation
}
Code snippet taken from "Complete C# Quick Reference - C# 12".