When you do list.count does it start counting from 0 or 1?

ex:

Element 0 = 1

Element 1 = 6

does it output 1 or 2?

I tried searching it in the documentation but I couldnt find the list page… If you can link it to me I would appreciate

tnx

When you do list.count does it start counting from 0 or 1?

ex:

Element 0 = 1

Element 1 = 6

does it output 1 or 2?

I tried searching it in the documentation but I couldnt find the list page… If you can link it to me I would appreciate

tnx

It starts counting from zero. Otherwise, there would be zero things in there when there was one.

That’s a nonsensical question. A count is a count; it is nothing more or less than the number of items in the list. (If you were talking about the list *bounds*, then you could sensibly ask whether these start at 0 or 1.)

You seem to be describing a list with 2 elements. .Count for that list returns 2.

https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

2 Likes

A list with 2 elements has a List.Count == 2. Additionally, the last index in a list is always List.Count -1, since the first element will have an index of 0. I’m guessing that is where the confusion is coming from. Example:

```
List<int> intList = new List<int>();
//intList will have a Count of 0, and no elements
intList.Add(20);
//intList will have a Count of 1, and a single element at index 0
intList.Add(100);
//intList will have a Count of 2, and two elements index 0 and index 1
intList.Add(300);
//intList will have a Count of 3, and 3 elements at indexes 0, 1, and 2
intList.Remove(100);
//intList will have a Count of 2, and 2 elements at indexes 0, and 1
//element 300 will have shifted from index 2 to index 1
```

3 Likes

Just to add a comment about documentation: when you need C# documentation, unrelated to unity, search for it on the web.

“list count c#” for instance … =)

1 Like

ok tnx… the starts counting from 1 really explains a lot of errors.

I think you had “count” confused with “index”

Index starts from 0. Think of it this way: If you’re looking for the first object in an array, the question is, “How many positions from the beginning should I move before I find the object?” Since the object is in the first position, the answer is 0.

Counting is… well, it’s counting. How many hands do you have? How did you start counting your hands? It works exactly like that.

3 Likes