Search⌘ K
AI Features

Lists

Explore the basics of C# lists in Unity including declaration, initialization, and key methods for manipulating data. Understand how to access elements, use loops to iterate, and follow best practices for efficient, readable code useful in AR development.

Introduction

Lists are a data structure in C# similar to arrays but with some added functionality. They are dynamic in size, which means that they can grow and shrink as needed, and they also offer several helpful methods for manipulating their contents. This lesson will explore the basics of lists in C#, how to use them, and some best practices for working with them.

Declaring and initializing lists

To create a list in C#, we first need to declare it and then initialize it with values. Here’s an example of how to do this:

C#
List<int> myNumbers = new List<int>();
myNumbers.Add(10);
myNumbers.Add(20);
myNumbers.Add(30);

In this example, we’ve declared a list called myNumbers that can store integers. We’ve then used the Add() method to add three values to the list: 10, 20, and 30.

We can also initialize a list with values in a single statement, like in the example given below:

C#
List<string> myStrings = new List<string>{"apple", "banana", "cherry"};

This operation ...