TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# object.ReferenceEquals Method

Use object.ReferenceEquals to compare objects. Test the performance of this method.
Object.ReferenceEquals. The object.ReferenceEquals method compares references. When you allocate an object, you receive a reference containing a value indicating its memory location in addition to the object's data on the memory heap.Object

Tip: The object.ReferenceEquals method gives you the ability to determine if 2 objects are in the same memory location.

Example. Two StringBuilders are allocated upon the managed heap. This occurs in the new StringBuilder() statements. The identifiers builder1 and builder2 are reference variables. They contain values that points to those locations on the heap.StringBuilder

Call 1: The first call to object.ReferenceEquals returns false because those 2 memory locations are different.

Call 2: The second call returns true because the references were set equal in an assignment.

Info: Assignments in the C# language perform a simple bitwise copy of the value. This makes the reference equivalent.

Calls 3 and 4: The two references literal1 and literal2 are equal. They point to the same memory location. String literals are pooled.

String Literalstring.Intern
C# program that uses object.ReferenceEquals using System; using System.Text; class Program { static void Main() { // Test object.ReferenceEquals. StringBuilder builder1 = new StringBuilder(); StringBuilder builder2 = new StringBuilder(); Console.WriteLine(object.ReferenceEquals(builder1, builder2)); builder1 = builder2; Console.WriteLine(object.ReferenceEquals(builder1, builder2)); // Test object.ReferenceEquals on string literals. string literal1 = "a"; string literal2 = "a"; Console.WriteLine(object.ReferenceEquals(literal1, literal2)); literal1 = literal2; Console.WriteLine(object.ReferenceEquals(literal1, literal2)); } } Output False True True True
Performance. Is object.ReferenceEquals a slow or fast method call? I developed this console program that tests the performance of object.ReferenceEquals on a custom class. It sees how fast the call is when the objects are equal, and when they are not.Benchmark

Info: ReferenceEquals was fast. It was a faster way of testing two objects for equality than comparing a unique Id field.

Thus: With objects similar to A, object.ReferenceEquals is not slow. We can use it without too much concern.

C# program that tests object.ReferenceEquals using System; using System.Diagnostics; class A { public int Id; } class Program { const int _max = 1000000000; static void Main() { bool same = true; A a1 = new A() { Id = 1 }; A a2 = same ? a1 : new A() { Id = 2 }; int x = 0; var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { if (object.ReferenceEquals(a1, a2)) { x++; } } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { if (a1.Id == a2.Id) { x++; } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.Read(); } } Result when same equals true 0.32 ns [object.ReferenceEquals] 0.64 ns Result when same equals false 0.64 ns [object.ReferenceEquals] 0.64 ns
References in the C# language refer to a location in memory. By comparing references you can determine if the two variables have an identical location in memory. The object.ReferenceEquals method is not always used.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf