Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
communitycreator

How to get the hash code of a decimal number in C#

Theodore Kelechukwu Onyejiaku

Overview

We can use the GetHashCode() decimal method to get the hash code of a decimal number in C#. This method is called on an object, which, in this case, is a decimal value.

A hash code is a unique numeric value that is used to identify an object.

Syntax

public override int GetHashCode ();

Parameters

This method doesn’t take any parameters.

Return value

This method returns the hash code of a decimal number.

Example

// use System
using System;
// create class
class HashGetter
{
    // create main method
    static void Main()
    {
      // create decimal
      decimal decimalNumberOne = 23.456m;
      decimal decimalNumberTwo = 1.33334m;
      decimal decimalNumberThree = 5.005m;

      // get hashcodes and printing them
      Console.WriteLine(decimalNumberOne.GetHashCode());
      Console.WriteLine(decimalNumberTwo.GetHashCode());
      Console.WriteLine(decimalNumberThree.GetHashCode());
    }
}
Getting HashCodes of decimal numbers in C#

Explanation

  • Lines 10 to 12: We instantiate and initialize three decimal variables.
  • Lines 15 to 17: We call GetHashCode() on the decimal values and print the results to the console.

RELATED TAGS

c#
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring