Ravi Vyas Ravi Vyas - 1 year ago 143
C# Question

How to Convert Dictionary<String, Object> to ArrayList in C# 3.5

I am passing the Dictionary parameter from the Javascript and trying to convert that to an Array List.


public void AssignEmployees(Dictionary<String, Object> _params)
ArrayList ArrayEx= null;
ArrayEx= (ArrayList)_params["Assignments"];

ArrayList ArrayEx1= null;
ArrayEx1= (ArrayList)_params["balance"];

This is a brief example and I have added the conditions which check if "_params" contains "Assignments" or "balance" then move forward.

However it returns an error of Unable to cast object of type
'System.Object[]' to type 'System.Collections.ArrayList'.

Here is the screenshot of an ideal data of this dictionary object.

enter image description here

Answer Source

You can iterate the Dictionary and use the .Add method of the ArrayList to insert the object to the array list. Or else try .AddRange method with Linq. Following code will help you:

ArrayList ArrayEx = new ArrayList();
foreach (var item in _params)

// or like this


Keep in mind, you can use the two methods to an Instantiated object of ArrayList class, so don't forget to initialize ArrayEx with an instance.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download