I am creating a State-Manager for a program. Each state will have a ID and a State class. I can add as many stats as I want by using an Add() function. Using a Change(ID) function will get change the state to the one in the list with a matching ID. Then ander further calls to the Manager will refer to the set state.
I would like to create a 2D list as such:
//short = ID No, IState = Sate Interface all states are based on
List<short, IState> StateList = new List<short, IState>();
I think for C# I'd require to create a List of Lists to get a 2D but I am unsure as the Intellisense marks it as List < T > with T being the param. Is there a better way of doing this? I appologise if this is very basic as I'm very new to this whole List idea.
Simply replace List
with Dictionary
. You want a key/value pair as your list item, which a Dictionary provides.
Dictionary<short, IState> StateList = new Dictionary<short, IState>();
You can then index into the dictionary to find the state you want:
// Retrieve State 5
var state5 = StateList[5];
Can you use a Dictionary object? These are great for name/value pairs.
You could implement it as
Dictionary<short, IState> StateList = new Dictionary<short, IState>();
then just do
StateList.Add(1, new State() { blah, blah });
You can create a List<Tuple<short, IState>>
if you want.
However, if the order of the items in the list is not important to you, you can use a Dictionary<short, IState>
.
If the order is important, but happens to be the ID (lower IDs come before higher IDs, for instance), you can use a SortedDictionary<short, IState>
.
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.