using System.Collections.Generic;
public static void Main()
var persons = new List<Person>
new Person{ Id = 1, Name = "Alice", Birthday = new DateTime(2002, 3, 17), Job = "Dentist", Salary = 30_000 },
new Person{ Id = 2, Name = "Bob", Birthday = new DateTime(2003, 3, 17), Job = "Doctor", Salary = 32_000 },
new Person{ Id = 3, Name = "Carol", Birthday = new DateTime(2004, 3, 17), Job = "Trainer", Salary = 33_000 },
new Person{ Id = 4, Name = "Dennis", Birthday = new DateTime(2003, 3, 17), Job = "Developer", Salary = 30_000 },
new Person{ Id = 5, Name = "Alice", Birthday = new DateTime(2004, 3, 17), Job = "Scientist", Salary = 33_000 },
Console.WriteLine(JsonSerializer.Serialize(GetCommonProperties(new[] { persons[0], persons[4] })));
Console.WriteLine(JsonSerializer.Serialize(GetCommonProperties(new[] { persons[1], persons[3] })));
Console.WriteLine(JsonSerializer.Serialize(GetCommonProperties(new[] { persons[2], persons[4] })));
private static T GetCommonProperties<T>(IEnumerable<T> source) where T : new()
var props = typeof(T).GetProperties();
foreach (var item in source)
foreach (var prop in props)
var value = prop.GetValue(item, null);
prop.SetValue(common, value);
foreach (var prop in props)
var itemValue = prop.GetValue(item, null);
var commonValue = prop.GetValue(common, null);
if ((dynamic)itemValue != (dynamic)commonValue)
prop.SetValue(common, GetDefault(prop.PropertyType));
private static object GetDefault(Type t)
.GetMethod(nameof(GetDefaultValue), BindingFlags.NonPublic | BindingFlags.Static)
private static T GetDefaultValue<T>()
public DateTime Birthday { get; set; }
public int Id { get; set; }
public string Job { get; set; }
public string Name { get; set; }
public int Salary { get; set; }