Override Equals Method Of Value Types
11 September 2015
Hello everybody.
Today I want to give some demo.
using System; using System.Diagnostics; namespace StackOverflowQuest { class Program { struct StructTest { public string TestString { get; set; } //public override bool Equals(object obj) //{ // var ct = (StructTest)obj; // return ct.TestString == this.TestString; //} } class ClassTest { public string TestString { get; set; } public override bool Equals(object obj) { var ct = obj as ClassTest; if (ct == null) return false; return ct.TestString == this.TestString; } } static void Main(string[] args) { StructTest st = new StructTest() { TestString = "water"}; StructTest st2 = new StructTest() { TestString = "water" }; ClassTest ct1 = new ClassTest() { TestString = "water" }; ClassTest ct2 = new ClassTest() { TestString = "water" }; int numberOfIterations = 500000; Stopwatch sw2 = new Stopwatch(); sw2.Start(); for (int i = 0; i < numberOfIterations; i++) { ct1.Equals(ct2); } sw2.Stop(); Console.WriteLine("class perfomance = {0} Elapsed Milliseconds", sw2.ElapsedMilliseconds); Stopwatch sw1 = new Stopwatch(); sw1.Start(); for(int i = 0; i < numberOfIterations; i++) { st.Equals(st2); } sw1.Stop(); Console.WriteLine("structs perfomance = {0} Elapsed Milliseconds", sw1.ElapsedMilliseconds); Console.ReadKey(); } } }
Take note of commented method of Equals method. If I execute that code on my machine, I'm getting difference in perfomance in 6 times. But if to uncomment Equals, then class and struct have comparable perfomance.
Ready to optimize your Acumatica experience? Just like fine-tuning the performance of your code, customizing Acumatica can unlock its full potential for your business. Whether it's streamlining processes, enhancing functionality, or integrating with other systems, we’re here to help. Leave a customization request today and let’s build a solution tailored to your unique needs. Your business deserves the best—let’s make it happen together!