In this article, we will discuss the Uri.ReferenceEquals method in C# with its syntax, parameters, and examples.
What is the Uri.ReferenceEquals method?
In C#, the Uri.ReferenceEquals method is not a standard method provided by the .NET Framework's Uri class. The ReferenceEquals method is a method from the C# Object class. It will give information on the Uri class .
The Uri class in C# belongs to the System namespace and represents a Uniform Resource Identifier (URI). It allows us to interpret, alter, and compare URIs easily. URIs can represent a variety of resources, including URLs and local file paths.
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 method gives a true value if two objects' references are equal; if not, it returns false.
Example 1:
Let us take an example to illustrate the Uri.ReferenceEquals method in C#.
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 us take another example to illustrate the Uri.ReferenceEquals method in 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
There are several advantages of the Uri.ReferenceEquals Method in C#. Some main advantages of the Uri.ReferenceEquals Method are as follows:
- Reference comparison:
The programmers may use the ReferenceEquals method to compare two objects at the reference level. It is useful for determining whether two variables reference the same object reference in memory.
- Null Handling:
The ReferenceEquals method handles null values effectively. If one or both of the items under comparison are null, the function returns true. It can make null-checking logic simpler in certain cases.
- Performance:
Reference equality tests are quicker than value equality checks because they directly compare memory locations. The ReferenceEquals method is a useful function for reference comparison in scenarios where speed is crucial.
- Memory Management:
Effective memory management requires understanding reference equality. It assists developers in ensuring that they are dealing with the correct object instances, especially for cases involving object pooling or duplicating instances.