-3

hi how could I make array like this in c#??: settings width and height is loaded from file

properties["settings"]["width"] = "bbb";
properties["settings"]["height"] = "cccc";

and dynamic_string_key is keys loaded from file and I dont know how many or what key name and values will be :)

properties["sets_of_data"][dynamic_string_key]= "lalala";
properties["sets_of_data"][dynamic_string_key]= "lalala";
properties["sets_of_data"][dynamic_string_key]= "lalala";
properties["sets_of_data"][dynamic_string_key]= "lalala";
Matas Lesinskas
  • 414
  • 6
  • 13
  • Use either a `Dictionary>`; but in my opinion it would be better to have some configuration/settings class/object that has two `Dictionary`, one for each the "settings" block and one for the "sets_of_data" block. Otherwise, your code might quickly become a mess with all those keys over two levels... –  Dec 07 '18 at 17:00
  • FTFY `Dictionary>` – Xiaoy312 Dec 07 '18 at 17:00
  • Your examples don't look like arrays you mean dictionaries/hashtables? – Wanton Dec 07 '18 at 17:00
  • probably Dictionaries, but for me its arrays :)(Iam php programmer) – Matas Lesinskas Dec 07 '18 at 17:01
  • 1
    Yeah, but C# is not PHP. If you want to stick with the PHP way of doing things, use PHP. Otherwise, in C# it is dictionaries... –  Dec 07 '18 at 17:02
  • @Xiaoy312, but I have to specify keys then I cant make random keys with dictionaries? can you give me example. I tryed this Dictionary> , but it didnt liked dynamic string keys that do not exist when I make dictionary – Matas Lesinskas Dec 07 '18 at 17:03
  • So what is best solution to this ? – Matas Lesinskas Dec 07 '18 at 17:04
  • Use `Dictionary>`. I did chose the pseudo-code names there for the string types to highlight their purpose. Sorry if that confused you... –  Dec 07 '18 at 17:04
  • 1
    It is not that "it didnt liked dynamic string keys", but the nested dictionary is not initialized when you try to use it. do this: `properties["a"] = new Dictionary(); properties["a"][dynamic_string_key] = "lalala";` note: only initialized it once, or you will lose data. – Xiaoy312 Dec 07 '18 at 17:08
  • For a better solution, use a tuple or value tuple as key in a normal dictionary as shown [here](https://stackoverflow.com/a/22244631/561113). – Xiaoy312 Dec 07 '18 at 17:09
  • @Xiaoy312 yea probably duplicate, but examples there is unclear for new users, with an example of a question like mine. I know I will get downvotes but is worth it. – Matas Lesinskas Dec 07 '18 at 17:14
  • Your question is little bit hard to understand other ways as well. You talk about reading data from file but then you are setting values. – Wanton Dec 07 '18 at 17:16

4 Answers4

2

Arrays in C# only allow you to find an element by the index (integer), not by an arbitrary string. In C#, that's a Dictionary<>.

You can use a dictionary of a dictionary, but it's not as easy:

var data = new Dictionary<string, Dictionary<string, string>>();
data["settings"] = new Dictionary<string, string>();
data["settings"]["width"] = "bbb";

But that seems overly complicated. If you know you'll have "settings", then it's probably more readable to just have one dictionary for settings:

var settings = new Dictionary<string, string>();
settings["width"] = "bbb";
Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84
1

If your file is JSON you can use JSON.NET and do it like this

JObject obj = JObject.Parse(File.ReadAllText("foo.json"));
var bar = (string)obj["foo"]["bar"];
Wanton
  • 800
  • 6
  • 9
0

you can use

Dictionary<string, Dictionary<string, string>> properties =
Dictionary<string, Dictionary<string, string>>
{
    {
        "settings",
        new Dictionary<string, string>
        {
            {"width", "200"},
            {"height", "150"},
        }
    }
};  
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
0

Or you can use Tuples, especially named tuples

var exampleNamedTuple = (Height: 0, Width: 0, Value: "");

var list = new List<(int Height, int Width, string Value)>();
list.Add(exampleNamedTuple);

var height = list.First().Height;
var width = list.First().Width;
var value = list.First().Value;

then you can assign it to your settings property.

https://learn.microsoft.com/en-us/dotnet/csharp/tuples