Getting the Hash Code of the Specified Range in C#
Last Updated :
28 Nov, 2019
The Range Structure is introduced in C# 8.0. It represents a range that has a start and end indexes. You are allowed to get the hash code of the specified range with the help of the GetHashCode() Method provided by the Range struct. This method returns the hash code of the specified instance.
Syntax:
public override int GetHashCode();
Example 1:
using System;
namespace range_example {
class GFG {
static void Main( string [] args)
{
var r1 = new Range(2, 4);
Range r2 = 1..10;
var r3 = Range.StartAt(4);
Console.WriteLine( "Hash Code of Range_1: " +r1.GetHashCode());
Console.WriteLine( "Hash Code of Range_2: " + r2.GetHashCode());
Console.WriteLine( "Hash Code of Range_3: " + r3.GetHashCode());
}
}
}
|
Output:
Hash Code of Range_1: -1254614029
Hash Code of Range_2: 853498667
Hash Code of Range_3: -1528050329
Example 2:
using System;
namespace range_example {
class GFG {
static void Main( string [] args)
{
int [] arr = new int [8] {100, 200, 300, 400,
500, 600, 700, 800};
var r = Range.StartAt(3);
var new_arr = arr[r];
Console.WriteLine( "Range: " + r);
Console.Write( "HashCodes: " );
foreach ( var i in new_arr)
Console.Write($ " [{i.GetHashCode()}]" );
}
}
}
|
Output:
Range: 3..^0
HashCodes: [400] [500] [600] [700] [800]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...