c#中PHP关联数组的最佳替代方法是什么? [英] What is the best alternative of PHP associative array in c#?
问题描述
我有一个php项目。现在我正在将这个项目转换为Asp.net Mvc。在我的php项目中,我有一个关联数组,如
$ data [' authorizationModules'] = array(' 01' => ' 应用用户授权',
' 02' => ' 设备授权,
' 03' => < span class =code-string>' Pin Reset Authorization',
' 04' => ' 管理员用户授权' 跨度> ,
' 05' => ' 管理员用户组授权',
' 06' => ' 限制包授权');
现在我想要将此代码转换为c#的最佳方法。此外,我需要将此数组从我的控制器传递到视图,我需要在视图中循环数组以填充下拉列表。
< 选择 >
< 选项 value = 01 > 应用用户授权< / option < span class =code-keyword>>
< / select >
所以任何人都可以建议我怎么做?
data [' authorizationModules'] = array(' 01' => ' 应用用户授权',
' 02' => ' 设备授权',
' 03' => ' Pin Reset Authorization',
' 04' => ' 管理员用户授权',
' 05' => ' 管理员用户组授权',
' 06' => ' 限制包授权');
现在我想要将此代码转换为c#的最佳方法。此外,我需要将此数组从我的控制器传递到视图,我需要在视图中循环数组以填充下拉列表。
< 选择 >
< 选项 value = 01 > 应用用户授权< / option < span class =code-keyword>>
< / select >
所以任何人都可以建议我怎么做?
我想,它不是替代,但模拟,而不是C#,但在.NET BCL中。这很容易回答:这是您的选择:
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 [ ^ ]。
这取决于你你如何在他们之间做出选择即使所有三种类型在搜索中都提供O(1)时间复杂度,但主要区别在于存储器消耗和性能之间的不同折衷。有些想法,请参阅:
http://stackoverflow.com / questions / 1427147 / sortedlist-sorteddictionary-and-dictionary [ ^ ],
http:// blog .bodurov.com / Performance-SortedList-SortedDictionary-Dictionary-Hashtable [ ^ ]。
参见:
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
您可以使用字典 [ ^ ]:
Dictionary< ; string,string> authorizationModules = new 字典<字符串,字符串>()
{
{ 01, 应用用户授权 },
{ 02, 设备授权},
...
};
Quote:我需要循环遍历数组
你可以像这样循环一个字典:
foreach (KeyValuePair< string,string> pair in authorizationModules)
{
string key = pair.Key;
string value = pair.Value;
// ...
}
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
$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.
<select>
<option value="01">Apps Users Authorization</option>
</select>
So can anyone suggest me how can I do this?
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.
<select> <option value="01">Apps Users Authorization</option> </select>
So can anyone suggest me how can I do this?
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
You can use a Dictionary[^]:
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:
foreach (KeyValuePair<string, string> pair in authorizationModules) { string key = pair.Key; string value = pair.Value; // ... }
这篇关于c#中PHP关联数组的最佳替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!