I am working on some projects to better acclimate myself to C#. I have this code example below:
Hashtable hashtable = new Hashtable();
for(int i = 0; i < s.Length; i++)
{
if(hashtable.ContainsKey(s[i]))
{
hashtable[s[i]] = (int)hashtable[s[i]] + 1;
}
else
{
hashtable.Add(s[i], 1);
}
}
for(int i = 0; i < s.Length; i++)
{
if((int)hashtable[s[i]] == 1)
return s[i];
}
return '\0';
I need to access the element at a specified key and update it by adding 1 to it. When I try to convert the object stored in the hashtable to an integer, it gives me this warning:
warning CS8605: Unboxing a possibly null value.
warning CS8605: Unboxing a possibly null value.
2 Warning(s)
0 Error(s)
Is there a way to avoid this warning? I do not think it would be possible for a NULL to appear in my hashtable, but it is warning me just in case. Is there a way to access this value in the hashtable without unboxing it?