Search Unity

Container boundary check on C#?

Discussion in 'Scripting' started by egoquat, Aug 6, 2020.

  1. egoquat

    egoquat

    Joined:
    Jul 4, 2012
    Posts:
    93
    Hello. what is a common way to check the boundary of C# array or list<> officially?
    It seems to be not supported official MS documents. Thanks.
    I expected like that
    int [] arr = new int[105];
    bool iterate(int idx)
    {
    if (false == arr.RangedIn(idx))
    return false;
    .....
    }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    myArr.Length
    will tell you the length of an array.
    myList.Count
    will tell you the number of elements in a list.

    Code (CSharp):
    1. int myIndex;
    2. bool indexIsInList = myIndex > 0 && myIndex < myList.Count;
    3. bool indexIsInArray = myIndex > 0 && myIndex < myArray.Length;