Does C# have any equivalent of PHP's array_key_exists function?
For example, I have this PHP code:
$array = array();
$array[5] = 4;
$array[7] = 8;
if (array_key_exists($array, 2))
echo $array[2];
How would I turn this into C#?
Sorry, but dynamic arrays like PHP are not supported in C#. What you can do it create a Dictionary<TKey, TValue>(int, int) and add using .Add(int, int)
using System.Collections.Generic;
...
Dictionary<int, int> dict = new Dictionary<int, int>();
dict.Add(5, 4);
dict.Add(7, 8);
if (dict.ContainsKey(5))
{
// [5, int] exists
int outval = dict[5];
// outval now contains 4
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With