Dictionaries

When we talk about collection data, we need to mention Dictionaries. Dictionary is similar to a List. However, instead of accessing a certain element by index value, we use a string called Key.

The Dictionary that you will probably be using most often is called Hashtable. Feel free to dive into the C# documentation after reading this chapter to discover all the bits of this powerful class.

Here are a few key properties of Hashtable:

  • Hashtable can be resized dynamically like List<T> and ArrayList
  • Hashtable can store multiple data types at the same type, like ArrayList
  • A public member Hashtable isn't visible in the Unity Inspector panel due to default inspector limitations

I want to make sure that you won't feel confused, so I will go straight to a simple example:

Dictionaries

Adding elements into hashtable must contain a string with the key. The key is necessary for retrieving a specific value. We have mentioned this before but I want to highlight the main difference between ArrayList and Hashtable. In ArrayList, data is stored under the index integer number value. In Hashtable, however, we store a value under the string key.

The Add function of Hashtable is taking two parameters here. Take a look at line 11. This line adds the value "Greg" under the "firstName" key. Simple, right? If you are confused, all you need to remember now is that when you want to add a value to Hashtable, you start with the Hashtable type variable name, followed by a dot and Add. Then, in the brackets, you enter the string key followed by a comma and any type of data key.

Accessing values

To access a specific key in the Hashtable, you must know the string key the value is stored under. Remember, the key is the first value in the brackets when adding an element to Hashtable. Ideally, you should also know the type of data you are trying to access. In most cases, that would not be an issue. Take a look at this line. Try to stay calm and do not panic!

Debug.Log((string)personalDetails["firstName"]);

Similar to ArrayList, we can store mixed-type data in Hashtable. Unity requires the developer to specify how an accessed element should be treated. To do this, we need to cast the element into a specific data type. The syntax is very simple. There are brackets with the data type inside, followed by the Hashtable variable name. Then, in square brackets, we have to enter the key string the value is stored under. Ufff, confusing!

As you can see in the preceding line, we are casting to string (inside brackets). If we were to access another type of data, for example, an integer number, the syntax would look like this:

(int)personalDetails["age"];

I hope that this is clear now. If it isn't, why not search for more examples on the Unity forums?

How do I know what's inside my Hashtable?

Hashtable, by default, isn't displayed in the Unity Inspector panel. You cannot simply look at the Inspector tab and preview all keys and values in your public member Hashtable.

We can do this in code, however. You know how to access a value and cast it. What if you are trying to access the value under a key that isn't stored in the Hashtable? Unity will spit out a null reference error and your program is likely to crash.

To check whether an element exists in the Hashtable, we can use the .Contains(object) method, passing the key parameter:

How do I know what's inside my Hashtable?
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset