In this guide, we will explore the Uri.ReferenceEquals method in C# along with its syntax, parameters, and demonstrations.
What is the Uri.ReferenceEquals method?
In C#, the Uri.ReferenceEquals method is not a built-in method included in the Uri class of the .NET Framework. This method belongs to the Object class in C# and can be used to obtain details about instances of the Uri class.
The Uri class within C# is a part of the System namespace and serves as a representation of a Uniform Resource Identifier (URI). Its purpose is to facilitate the interpretation, modification, and comparison of URIs in a straightforward manner. URIs have the capability to denote a wide range of resources, such as URLs and paths to local files.
Syntax:
It has the following syntax:
bool Uri.ReferenceEquals(Uri uri_1, Uri uri_2)
Parameters:
uri_1: It is the initial URI to compare.
uri_2: It is an additional URI to compare.
Return Value:
This function returns a boolean true if the references of two objects are identical; otherwise, it returns false.
Example 1:
Let's consider an instance to demonstrate the Uri.ReferenceEquals function in the C# programming language.
using System;
using System.Globalization;
class ReferenceEqual{
// Main Method
public static void Main()
{
// the initial value1 declaration
Uri a_1 = null;
// the initial value2 declaration
Uri a_2 = null;
// the ReferenceEquals(Uri ,
// Uri ) method
bool result = Uri.ReferenceEquals(a_1, a_2);
//The condition to check
if (result)
Console.WriteLine("The null value is equal to the null");
else
Console.WriteLine("The null value is not equal to the null");
}
}
Output:
The null value is equal to the null
Example 2:
Let's consider another instance to demonstrate the Uri.ReferenceEquals method within C#.
using System;
using System.Globalization;
class ReferenceMethod {
// Main Method
public static void Main()
{
Uri a1 = new Uri("https://www.hello world.com/index.htm");
Uri x1 = null;
// the get() method
get(a1, null);
// the value of a1 is assigned to x1
x1 = a1;
get(a1, x1);
get(x1, null);
}
// the get() method for result
public static void get(Uri va_1, Uri va_2)
{
//the ReferencesEquals() method
bool result = Uri.ReferenceEquals(va_1, va_2);
// the result condition
if (result)
Console.WriteLine("{0} is equal to {1}", va_1, va_2);
else
Console.WriteLine("{0} is not equal to {1}", va_1, va_2);
}
}
Output:
https://www.hello world.com/index.htm is not equal to
https://www.hello world.com/index.htm is equal to https://www.hello world.com/index.htm
https://www.hello world.com/index.htm is not equal to
Advantages of the Uri.ReferenceEquals Method
Reference comparison is a key benefit of the Uri.ReferenceEquals Method in C#. This method allows for comparing references directly.
Utilizing the ReferenceEquals method allows developers to compare objects at the reference level. This method proves beneficial in establishing if two variables point to the same object in memory.
- When it comes to null handling:
The ReferenceEquals function effectively manages null values. When either one or both of the items being compared are null, the method will yield true. This can streamline the process of checking for null values in specific scenarios.
- Efficiency:
Reference equality comparisons are faster than value equality validations as they directly assess memory addresses. The ReferenceEquals function proves to be valuable for comparing references in situations where efficiency is of utmost importance.
- Memory Management:
A solid grasp of reference equality is essential for efficient memory management. It aids programmers in verifying the precise object instances they are working with, particularly in scenarios like object pooling or duplicating instances.