C# - Basics - Types - Comparing Reference Types ReferenceEquals()
Nisan 4, 2011 by C# Tutorial
|
|
Article Information
This topic contains and tries to cover following subjects:
- Explanation of Comparing Reference Types in C#
- Example of Comparing Reference Types in C#
Articles tries to provide answer to following questions and issues:
- How to compare two reference type object to check equality in C#?
Articles pre-requisites following information:
- General knowledge of Reference Type and Value Types
- System.Object and its inheritance
Explanation of Comparing Reference Types in C#
In C#, to check the two reference type object-instance, the method ReferenceEquals() can be used. This method returns TRUE/FALSE depending on comparison result. It tests whether the two references refer to same addressee in Object.ReferenceEquals function also can compare null to an object.
In normal case, if we compare two value type as follows:
int x = 7;
int y = 9;
ReferenceEquals (x, y) //returns FALSE...///
As two value type, contained in separate addresses within memory method returns FALSE.
What happens if we check two reference type object? it returns FALSE as well. Even two instances are reference type, they are stored in heap in different places.
object x = 7;
object y = 9;
ReferenceEquals (x, y) //returns FALSE...///
For scenario, lets add one more object and assign object y to it:
object x = 7;
object y = 9;
object z = y;
ReferenceEquals (z, y) //returns TRUE...///
In above assignment, both objects referencing same addressee. In other word, if we change value of y, z will be changed automatically.
Example of Comparing Reference Types in C#
Following example demonstrates how to compare two reference type objects to check if they reference to same addressee. Example is a console application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NS_exampleApp
{
//we create a class for employee records...///
class CL_employees
{
public string V_empyloyeeName;
}
class CL_x
{
static void Main(string [] args)
{
//we create an object for helen...///
CL_employees o_anEmployee = new CL_employees();
o_anEmployee.V_empyloyeeName = "helen";
//we create an object for william...///
CL_employees o_anotherEmployee = new CL_employees();
o_anotherEmployee.V_empyloyeeName = "william";
//lets check if helen and william objects reference to same memory adresse...///
Console.WriteLine("References are equal...?\n" + ReferenceEquals(o_anEmployee, o_anotherEmployee));
//lets assign william object to helen object and check again...///
o_anotherEmployee = o_anEmployee;
Console.WriteLine("References are equal...?\n" + ReferenceEquals(o_anEmployee, o_anotherEmployee));
Console.ReadLine();
}
}
}
Output:
As output indicates, assigning one reference type into another caused equality between reference types. In other word, if we change value of one, for example name of employee, other is affected too.
ReferenceEquals() method is a static method. Override is not supported. Therefore, System.Object implementation is default and only to use.
Data Layers
Area: | programming \ Languages \ csharp \ \ \ |
Ref: | http://msdn.microsoft.com/en-us/library/system.object.referenceequals.aspx |
Loc: | articles |
Tags: | csharp |
|