0

I have an array of structure objects initialized like below:

struct1 SettingList[] = { {"as","bs","cs"} , {"ak","bk",ck"} }

struct1 is defined as follows:

struct1{char[] str1; char[] str2; char[] str3}

I was translating some C++ code to C# and in the c++ code I saw an object accessed like below:

SettingList["as"].str1

How can you access an object with the first member of that object? How would I do that in c# without having to specify SettingList[0].str1

0

2 Answers 2

1

You could use a dictionary instead of a map.

var settingsList = yourArray.ToDictionary(i => i[0]);
var foo = settingsList["as"];
Sign up to request clarification or add additional context in comments.

2 Comments

I assume you mean array, not "map".
@Servy no i do mean a dictionary per " was translating some C++ code to C# and in the c++ code I saw an object accessed like below:"
0

If you want to find the struct that has the given string as the first of the values you can use this:

var value = SettingList.First(setting => setting[0] == "as").str1;

If you want to use the [] operator to do this then you'll need to create your own type that holds onto such an array and that does something like the above in the overloaded [] operator implementation.

1 Comment

Or use KeyedCollection<TKey, TValue>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.