-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"; 
12
  • Use either a Dictionary<string_FirstOuterKey, Dictionary<string_SecondInnerKey, string>>; but in my opinion it would be better to have some configuration/settings class/object that has two Dictionary<string, string>, 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... Commented Dec 7, 2018 at 17:00
  • FTFY Dictionary<string, Dictionary<string, string>> Commented Dec 7, 2018 at 17:00
  • Your examples don't look like arrays you mean dictionaries/hashtables? Commented Dec 7, 2018 at 17:00
  • 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... Commented Dec 7, 2018 at 17:02
  • 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<string, string>(); properties["a"][dynamic_string_key] = "lalala"; note: only initialized it once, or you will lose data. Commented Dec 7, 2018 at 17:08

4 Answers 4

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"; 
Sign up to request clarification or add additional context in comments.

Comments

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"]; 

Comments

0

you can use

Dictionary<string, Dictionary<string, string>> properties = Dictionary<string, Dictionary<string, string>> { { "settings", new Dictionary<string, string> { {"width", "200"}, {"height", "150"}, } } }; 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.