CopyPastehas never been so tasty!

Generate hashcode from string array in C#

by anonymous

  • 0
  • 0
  • 0
97 views

Generate hashcode from string array in C#

GetHashCode methodGetHashCode () is a method of object class and it is located in mscorlib assembly. It is used to generate hashcode from strings or even from bytes.String ArrayString Arrays are used to store more than one string. We create and declare string arrays like that: 

string[] arr1 = new string[] { "one", "two", "three" }; 

  Generating hashcode from a string arrayThe following code will demonstrate that how we can generate hashcode from string array 

Code

 using System;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Collections;

using System.IO;

usingSystem.Text.RegularExpressions;

  class Program

    {

static void Main(string[] args)

        {

int hashcode = GenerateHashCode("A","B","C");

 Console.WriteLine(hashcode);

Console.ReadLine();

         }

public static int GenerateHashCode(params string[] values)

        {

 int hashcode = 0;

foreach (string value in values)

            {

 if (value != null)

 hashcode += value.GetHashCode();

            }

 return hashcode;

         }

    }

Add A Comment: