Home > database >  Multi string array to multi dictionary in C#?
Multi string array to multi dictionary in C#?

Time:01-17

I got stuck to change the following array to a dictionary.

 public static string[][,] patterns = new string[][,]
 {
      new string[,] {
           { "1,2,3" },
           { "3,2,5" },
      },

      new string[,] {
           { "4,4,3" },
           { "7,1,2" },
      },
 
 };

This is what I have:

 public Dictionary<string, string[]> patterns = new Dictionary<string, string[]>();

I can't fill the array with predefined values.

I want to change to a dictionary, because it has a key.

Can I also change the above array to a key and values format?

CodePudding user response:

You can just make it a dictionary with a list, and I guess that covers your requirements (index access, variable number of integers for the index element). Here is an example (for the value list I am not sure, whether you really want a string or ints, in case just change the type):

// define dictionary    
IDictionary<string, IList<int>> dict = new Dictionary<string, IList<int>>();
    
// assign values
dict["abc"] = new List<int> { 2, 4, 8 };
dict["def"] = new List<int> { 10, 12, 14 };

// get value    
int dictDef2 = dict["def"][1];

CodePudding user response:

Finally, I got it.

public static Dictionary<string, string[]> demo = new Dictionary<string, string[]>
{
    { "abc",
        new string[]
            {
            "1", 
            "2"
            }
    },

    { "def",
        new string[]
            {
            "3",
            "4"
            }
    },
};

Contains check:

if (demo.ContainsKey("abc"))
{
}

Get the value(s):

demo["abc"][0]

Thanks for any help.

CodePudding user response:

If you want to change existing array, you can try Linq:

using System.Linq;

...

public static string[][,] patterns = new string[][,] {...}

...

public Dictionary<string, string[]> patternsDict = patterns
  .Select((value, index) => (
     key : $"keyNameExample{index}", 
     value : value.OfType<string>().ToArray()))
  .ToDictionary(pair => pair.key, pair => pair.value);

Note, that we have to convert (flatten) 2d array into ordinal one

If you want just to declare dictionary and fill it:

public Dictionary<string, string[]> patterns = new Dictionary<string, string[]>() {
  {"keyExample1", new string[] { "1,2,3", "3,2,5" }},
  {"keyExample1", new string[] { "4,4,3", "7,1,2" }},
};

CodePudding user response:

public string[][,] patterns = new string[][,] { new string[,] { { "1,2,3" }, { "3,2,5" } }, new string[,] { { "4,4,3" }, { "7,1,2" } } };

Dictionary<string, string[,]> patternsDictionary = new Dictionary<string, string[,]>();

            for (int i = 0; i < patterns.Length; i  )
            {
                patternsDictionary.Add(i.ToString(), patterns[i]);

            }
           
            Console.WriteLine(patternsDictionary["0"][0,1]);  // Returns  2 - from { "1,2,3" }

CodePudding user response:

can make it even shorter like:

public static Dictionary<string, string[]> demo = new Dictionary<string, string[]>
            {
                { "abc", new[]{"1","2"}},
                { "def", new[]{"3","4"}},
            };

and with C# 9 you can even do:

public static Dictionary<string, string[]> demo = new()
            {
                { "abc", new[]{"1","2"}},
                { "def", new[]{"3","4"}},
            };
  •  Tags:  
  • Related