// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; public struct Data1 { public int Iv; public string Sv; } public class Data2 { public int Iv { get; set; } public string Sv { get; set; } } public record class Data3 { public int Iv { get; set; } public string Sv { get; set; } } public class RecDet { public static void Main(string[] args) { Console.WriteLine("**** struct ****"); Data1 s1 = new Data1(); s1.Iv = 123; s1.Sv = "ABC"; Console.WriteLine("Value type: " + typeof(Data1).IsValueType); Data1 s2 = new Data1(); s2.Iv = 123; s2.Sv = "ABC"; Console.WriteLine("Equals: " + s1.Equals(s2)); Console.WriteLine("Same hash code: " + (s1.GetHashCode() == s2.GetHashCode())); Data1 s3 = s1; s1.Iv = 0; Console.WriteLine($"After assignment update: {s1.Iv} {s3.Iv}"); Console.WriteLine("**** class ****"); Data2 c1 = new Data2(); c1.Iv = 123; c1.Sv = "ABC"; Data2 c2 = new Data2(); c2.Iv = 123; c2.Sv = "ABC"; Console.WriteLine("Equals: " + c1.Equals(c2)); Console.WriteLine("Same hash code: " + (c1.GetHashCode() == c2.GetHashCode())); Data2 c3 = c1; c1.Iv = 0; Console.WriteLine($"After assignment update: {c1.Iv} {c3.Iv}"); Console.WriteLine("**** record ****"); Data3 r1 = new Data3(); r1.Iv = 123; r1.Sv = "ABC"; Console.WriteLine("Value type: " + typeof(Data3).IsValueType); Data3 r2 = new Data3(); r2.Iv = 123; r2.Sv = "ABC"; Console.WriteLine("Equals: " + r1.Equals(r2)); Console.WriteLine("Same hash code: " + (r1.GetHashCode() == r2.GetHashCode())); Data3 r3 = r1; r1.Iv = 0; Console.WriteLine($"After assignment update: {r1.Iv} {r3.Iv}"); } }