Get hash code of a String in C#

In this article we will discuss about how to get the hash code of any kind of String in C#.
  • 5696

If we want to get the hash code of any string then we have to use GetHashCode() method. The hash code is an encrypted code which is used for security purpose. We can get the hash code of any kind of data in C#. This function resised in System.Object namespace.

Here is the complete program in C#:

 

namespace ConsoleApplication8

{

    class Program

    {

        static void Main(string[] args)

        {

            DisplayHashCode("");

            DisplayHashCode("p");

            DisplayHashCode("pa");

            DisplayHashCode("pas");

            DisplayHashCode("pass");

            DisplayHashCode("passw");

            DisplayHashCode("passwo");

            DisplayHashCode("passwor");

            DisplayHashCode("password");

            Console.ReadLine();

        }

        static void DisplayHashCode(String Operand)

        {

            int HashCode = Operand.GetHashCode();

            Console.WriteLine("The hash code for \"{0}\" is: 0x{1:X8}, {1}",

                              Operand, HashCode);

        }

    }

}

Ask Your Question  

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.