How to classify an undefined vector list item?

1162 views c#

I understand that an undefined vector3 = default(vector3), but what about a vector list item that does not exist? For example:

List<Vector3> stuff = new List<Vector3>();
for(int i=1; i<10; i++){
stuff.add(new Vector3(0 + i, 0, 0));

In this case what would stuff[15] be considered?

answered question

Are you asking what happens if you try to access an index outside the bounds of the List?

Yes. I know you get "Argment is out of range", but what do you call it? Like if(stuff[15] == undefined){what would undefined be}

stuff[15] doesn't exist. It's an exception. Since you have not added 15 elements, but only 10, it can not access the 16 element, or at the 15th index

1 Answer


Assuming you are asking if your code will be able to access stuff[15], no.

The reason for this is because you only add 10 elements to it, each of type vector.

List<Vector3> stuff = new List<Vector3>();
for (int i = 0; i < 10; i++) {
    stuff.add(new Vector3(0 + i, 0, 0));

This code will add a new Vector 10 times with the positions given, where the first parameter is 0 + i. Since there are only 10 elements, stuff[15] will not be accessed, and will throw an ArrayOutOfBoundsException.

To safely check if an array can contain a 15th element, use an if statement

if (stuff.Length > 15) {
    stuff[15]; // will only be called if an element exists

posted this

Have an answer?


Please login first before posting an answer.