Click here to Skip to main content
16,021,181 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a php project. Now i am converting this project to Asp.net Mvc. In my php project i have an associative array like

PHP
$data['authorizationModules'] = array('01' => 'Apps Users Authorization',
                                      '02' => 'Device Authorization',
                                      '03' => 'Pin Reset Authorization',
                                      '04' => 'Admin User Authorization',
                                      '05' => 'Admin User Group Authorization',
                                      '06' => 'Limit Package Authorization');


Now i want the best way to convert this code into c#. Also i need to pass this array from my controller to the view and i need to loop through the array in the view to populate a drop-down.

HTML
<select>
    <option value="01">Apps Users Authorization</option>
</select>



So can anyone suggest me how can I do this?
Posted

I guess, it's not "alternative", but "analog", and not in "C#", but in .NET BCL. This is easy to answer: here is your choice:
https://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/f7fta44c(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/ms132319(v=vs.110).aspx[^].

It's up to you how you choose between them. Even though all three type provide O(1) time complexity in the search, the major differences are in the different trade-off between memory consumption and performance. For some food for thought, please see:
http://stackoverflow.com/questions/1427147/sortedlist-sorteddictionary-and-dictionary[^],
http://blog.bodurov.com/Performance-SortedList-SortedDictionary-Dictionary-Hashtable[^].

See also:
https://en.wikipedia.org/wiki/Big_O_notation[^],
https://en.wikipedia.org/wiki/Time_complexity#Table_of_common_time_complexities[^],
https://en.wikipedia.org/wiki/Computational_complexity_of_mathematical_operations[^].

—SA
 
Share this answer
 
v3
You can use a Dictionary[^]:
C#
Dictionary<string, string> authorizationModules = new Dictionary<string, string>()
{
    { "01", "Apps Users Authorization"},
    { "02", "Device Authorization"},
    ...
};

Quote:
i need to loop through the array

You can loop over a Dictionary like this:
C#
foreach (KeyValuePair<string, string> pair in authorizationModules)
{
    string key = pair.Key;
    string value = pair.Value;
    // ...
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900