public readonly struct User {
public readonly string Name;
public readonly int Age;
public User() {
Name = "<no name>";
Age = -1;
}
}
// ...
static void PrintStructDetails<T>() where T : struct {
void EnumerateFieldsOnToConsole(T instance, [CallerArgumentExpression("instance")] string? instanceName = null) { // If you're confused about this line, see the "Caller Argument Expressions" section below
foreach (var field in typeof(T).GetFields()) {
Console.WriteLine($"Field '{field.Name}' in {instanceName}: {field.GetValue(instance)}");
}
}
var newedInstance = new T();
var defaultInstance = default(T);
EnumerateFieldsOnToConsole(newedInstance);
EnumerateFieldsOnToConsole(defaultInstance);
}
Code snippet taken from "Complete C# Quick Reference - C# 10".