SoFunction
Updated on 2025-03-08

Comparison of common data structures in C# programming (Unity3D game development)

one. Preface

Unity3D is the most popular game development engine today, which allows us to easily create interactive content such as 3D video games, architectural visualization, real-time 3D animations, etc. It supports 2D/3D game development. According to incomplete statistics, 80% of domestic mobile games are currently developed using Unity3D.

Since Unity3D uses the most C# language during the development process, it is very necessary to reasonably use some data structures provided by C#. Reasonable selection of data structures can speed up development speed and improve game running performance. Unreasonable use of data structures will lead to reduced game running performance and increase development complexity!

Let’s first look at the commonly used data structures through the table:

Commonly used data structures of C#

Chinese name

Array

Normal array

ArrayList

Array collection

List<T>

Generic arrays

LinkList<T>

Generic Link List

Stack

Stack

Queue

queue

HashTable

Hash table

Dictionary<K,T>

dictionary

Then first understand the usage of these data structures.

two. Comparison between data structures and partial use cases

1. Arrays are more common in general programming and are one of the simplest data structures. They have three characteristics: the data stored in the array is on continuous memory units, the elements in the array are of the same type, and the array can be directly accessed through the subscript. The disadvantage is that arrays are stored continuously, and it is inconvenient to insert new elements between two elements. When creating an array, you must specify the length or initialize the elements, which will face the problem of overflow or memory waste. This can solve some of the disadvantages brought by Array: there is no need to specify the size or initialize the elements. Data elements can be inserted dynamically without considering overflow or waste of memory. The reason why ArrayList can store different data types is that because all types are treated as Object types, there is a high possibility of type mismatch when used. This means that ArrayList is a data structure with type insecure. It will incur additional overhead during frequent reading and writing (packing and unboxing) operations on ArrayList, resulting in performance degradation, so there is less ArrayList used during development!

3. In order to solve the disadvantages of ArrayList's unsafe type and boxing and unboxing, List<T> was introduced as a new array type, inheriting some advantages of ArrayList and ensuring the safety of the type. Since there is no need to box and unboxing operation, List<T> is high-performance, so List<T> is often used in game development. Here is a simple operation of List<T>:

List<string> Lstr=new List<string>();

("Element One"); // Insert data element

Lstr[0]="Element Two"; // Modify the data element

(0); // Remove data elements

In Unity3D game development, we can use List<GameObject> to store game objects, such as bullets in shooting games, or NPCs in role-playing games, and can use List<GameObject> to store them.

The characteristic of <T> is that each element in the chain points to the next element, thus forming a chain. The time complexity is O(1) when inserting and deleting a data element.

(Stack) is characterized by first in and then out.

(Quad) is characterized by first-in, first-out.

7. I introduce the use of hash tables and some of its features through usage in game development. During the game development process, we usually involve correctly matching hero characters through the ID of the game character, so each hero character must have its own unique ID. The ID of each hero character corresponds one by one to their character name. Then, we can use a hash table to store this data, assuming that there is the following information:

Serial number

ID

Role name

1

2059

Arthur

2

“2060”

Daji

3

“ABC”

Houyi

4

2061.5

Miyamoto Musato

The code stored using a hash table is as follows:

Hashtable Hero = new Hashtable ();

     //  (key,value);

      (2059,"Arthur");    //Insert the first element
      (“2060”,"Daji");    //Insert the second element
      (“ABC”,"Houyi");    //Insert the third element
      (2061.5,"Miyomoto Musashi");  //Insert the 4th element
 

     // Access value through key
     if ( (“2060”)) {

       ("The team contains Daji");

     } else {

      ("The team does not include Daji");

     }

Through the above code, we can find that the hash table can receive any type value as key (both key and value are both of the object type), which actually reflects that the hash table type is insecure.

8. To solve the insecure of hash table type, we can use Dictionary<K,T> to store data, and the above code can be rewrited into the following code:

Dictionary&lt;int ,string&gt; Hero = new Dictionary&lt;int, string&gt; ();

//  (int key,string value);

 (2059,"Arthur"); //Insert the first element
 (2060,"Daji"); //Insert the second element
 (2061,"Houyi"); //Insert the third element
 (2062,"Miyomoto Musashi"); //Insert the 4th element
// Access value(string) through key(int)
if ( (2061)) {

 ("The team contains Houyi");

} else {

 ("The team does not include Houyi");

}

Compared with the hash table, dictionaries ensure the security of types, but there is no perfect thing in the world, and the same is true for code. Dictionaries exchange space for time and use more memory overhead to satisfy their pursuit of speed. When creating a dictionary, you can pass in a dictionary capacity value, but in actual use it is not the value, but the minimum prime number not less than the value is its actual capacity, so the minimum value of the dictionary capacity is 3. And when the actual capacity is available, the index is not directly implemented, but indirect indexing is achieved by creating two additional Array arrays. The situation I face is that even if an empty dictionary is created, two arrays of length 3 are accompanied by. Therefore, when there is not much data processed, use dictionaries carefully.

three. Summarize

In the process of game development, it is also possible to use ordinary arrays in many cases. The selection of data structures requires the data size and usage scenarios to reasonably store and process the data. Excellent programmers need to consider the performance of machine operation. Choosing some data structures reasonably can improve program operation performance and reduce development complexity.

If you feel that you can learn something from the above knowledge, please share it with your friends. Thank you for your support.