Programming with C# and Unity 3D game engine – part 003 .

In this tutorials I will show how to deal with lists and enumerations.
First you need to make one C# script under Unity 3D. If don’t know how to do it then read this tutorial.
The list is an object which holds variables in a specific order.
The enumeration is an object with one distinct type that consists of a set of named constants called the enumerator list.
Let’s start with lists.
You need to put this on your script:

Now you can create list under your class , see the example with one class named YourClass:

We can see the List’s syntax is at first confusing. If you take a look to types – int, sting and GameObject and variables – ages, names and objects then the base remain this: List<...> … = new List<...>(); where types are into <> and variables can be created with = new … ().
Now will add one value to names with:

To reverse one list in-place then use this:

If you want then can remove one:

We can use an array to initialize a List named animals:

Loop into the list animals and print the list:

To get the number of elements from animals list, use this:

To clean the list just use this:

The next step is enumerations and is define by the enum type.
An enum type internally contains an enumerator list like this example:

In this enum named animals, cat will be zero (unless given another value explicitly), dog will be one, and fox will be two …
To print out the enum values, you can call ToString on the enum variable in a program.
The Console.WriteLine can automatically call the ToString method.
Using GetValues method with foreach to returns an array that contains a value for each member of the enumType enumeration.

Sometimes we have a string value that we want to convert to an equivalent enum and we can use Parse().
Also you can used many enum like: Enum.Parse, Enum.TryParse, Enum.Format, Enum.IsDefined…
You can get values from enum , see the example:

Convert enums to list:

admin

born 1976

Posted in C#, Game engine, Unity 3D, Visual Studio, Windows 10. Tagged with , , , , , , .