static void OperatorOverloading() { ProductBin pbWidgets1 = new ProductBin { Contents = "Widgets", Count = 5 }; ProductBin pbWidgets2 = new ProductBin { Contents = "Widgets", Count = 6 }; ProductBin pbSpecialWidgets = new ProductBin { Contents = "SpecialWidgets", Count = 3 }; var pbFail = pbSpecialWidgets + pbWidgets1; var pbSuccess = pbWidgets1 + pbWidgets2; }
public static ProductBin operator +(ProductBin pb1, ProductBin pb2) { if (pb1.Contents != pb2.Contents) { Console.WriteLine("ERROR: Cannot mix bins of different contents"); return(null); } var newPb = new ProductBin { Contents = pb1.Contents, Count = pb1.Count + pb2.Count }; Console.WriteLine("SUCCESS: Consolidated bin created"); return(newPb); }