I have a SortedList, where int is the key and value is a string.

public void AddElements() { int collectionLength = Int32.Parse(Console.ReadLine()); for(int key = 0; key < collectionLength; key++) internalSortedList.Add(key, RandomClass.CreateRandomString()); } 

The question is how to set a key as GetHashCode?

  • 2
    GetHashCode from what? You can write key.GetHashCode() , but it will not make sense, because The hash code from int is the int value itself, but you can do something in the spirit of String value = RandomClass.CreateRandomString(); internalSortedList.Add(value.GetHashCode(), value); String value = RandomClass.CreateRandomString(); internalSortedList.Add(value.GetHashCode(), value); - StateItPrimitive pm
  • thanks for the answer, it helped) apparently thought was not quite right, but you are right that it makes no sense to take the hashcode from int - cruim
  • Ok, duplicated in response :) - StateItPrimitive

1 answer 1

You can write key.GetHashCode() , but it will not make sense, because The hash code from int is the int value itself, but you can do something in the spirit of:

 String value = RandomClass.CreateRandomString(); internalSortedList.Add(value.GetHashCode(), value);