dictionary

How to convert Dictionary Keys or Values to Array in C#?

Say for example we declare

Dictionary tblCustomerWithIds = new Dictionary();

Convert the Keys to arrays as follows:-

int[] arrKeys = new int[tblCustomerWithIds.Keys.Count];

tblCustomerWithIds.Keys.CopyTo(arrKeys, 0);

Now arrKeys will have the int keys in the dictionary.

Convert the Values to arrays as follows:-

Customer[] arrCustomers = new Customer[tblCustomerWithIds.Values.Count];

tblCustomerWithIds.Values.CopyTo(arrCustomers, 0);

Now arrCustomers will have the Customer objects that are Values in the dictionary.