简体   繁体   中英

JavaScript to C# data structure

i am working on converting a Javascript code into C# to use it as ASP application,

i wonder equivalent data structure for the following javascript in C#

var ports = {
'critical': [21,3389,4444,3444],
'medium': [25],
'safe': [80,443]
};

You could use a C# dictionary. Here's my implementation of it:

Dictionary<string, int[]> dict = new Dictionary<string, int[]>()
        {
            {"critical", new int[] {21,3389,4444,3444} },
            {"medium", new int[] {25} },
            {"safe", new int[] {80,443} },
        };

        foreach( KeyValuePair<string, int[]> kvp in dict )
        {
            Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
        }

EDIT: Try using the following to find the key.

string key = dict.FirstOrDefault(x => x.Value.Contains(25)).Key;
Console.WriteLine(key);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM