Object-oriented programming is a programming paradigm that uses "objects" to design applications and software programs. In the context of C#, one of the fundamental pillars of object-oriented programming is the collection. Collections in C# are data structures that allow us to group objects more efficiently.

In C#, there are several types of collections, each with its own characteristics and uses. Let's explore some of the most common types of collections in C# and how they can be used in Unity game programming.

Array

Arrays are the most basic type of collection in C#. They allow you to store multiple elements of the same type in a single variable. For example, you can use an array to store a list of enemies in a game.

Enemy[] enemies = new Enemy[10];

Arrays are useful, but they are quite limited. They have a fixed size, which means you need to know how many elements you will store in the array at the time you create it. Furthermore, they do not provide many useful methods for manipulating the stored data.

List

Lists are a more flexible collection type in C#. They are similar to arrays, but have a dynamic size. This means you can add and remove elements from a list in real time. For example, you can use a list to store enemies in a game and add or remove enemies as needed.

List enemies = new List();

Lists also provide a variety of useful methods for manipulating stored data, such as Sort(), Reverse(), and Find().

Dictionary

Dictionaries are a type of collection that stores key-value pairs. They are useful when you need to associate values ​​with unique keys. For example, you can use a dictionary to store each player's score in a multiplayer game.

Dictionary scores = new Dictionary();

Dictionaries also provide a variety of useful methods for manipulating stored data, such as Add(), Remove(), and ContainsKey().

HashSet

A HashSet is a collection that stores unique elements. It is useful when you need to ensure that each element in a collection is unique. For example, you can use a HashSet to store a list of players in a multiplayer game, ensuring that each player is unique.

HashSet players = new HashSet();

HashSet also provides a variety of useful methods for manipulating the stored data, such as Add(), Remove() and Contains().

In short, collections in C# are powerful tools that can help make your games more efficient and organized. By understanding how and when to use each collection type, you can significantly improve the quality of your code and the efficiency of your game.

Now answer the exercise about the content:

Which of the following statements about collections in C# is true?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Object Orientation in C#: Delegates and Events

Next page of the Free Ebook:

29Object Orientation in C#: Delegates and Events

3 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text