Use custom class like array

Is it possible to use a custom class similar to an array, like accessing the children of transform, or the AnimationStates in a Animation object (which actually is really strange… you can access it by using a string?!)?

Here are examples of what I mean:

for (var child : Transform in transform) {
    child.position += Vector3.up * 10.0;
}

or

for (var state : AnimationState in animation) {
    state.speed = 0.5;
}

Transform implements a common interface called IEnumerable. This interface forces the class to provide a GetEnumerator() function which should return an object that implements the IEnumerator interface. With this enumerator you can "enumerate" through "whatever" the class want.

For example, this is how the Transform IEnumerator looks like:

// C#
public sealed class Transform : Component, IEnumerable
{
    private sealed class Enumerator : IEnumerator
    {
        private Transform outer;
        private int currentIndex = -1;
        public object Current
        {
            get { return this.outer.GetChild(this.currentIndex); }
        }
        internal Enumerator(Transform outer)
        {
            this.outer = outer;
        }
        public bool MoveNext()
        {
            int childCount = this.outer.childCount;
            return ++this.currentIndex < childCount;
        }
        public void Reset()
        {
            this.currentIndex = -1;
        }
    }

    public IEnumerator GetEnumerator()
    {
        return new Transform.Enumerator(this);
    }

    // Rest of Transform
}

Now if you use a transform in a foreach-loop the compiler sees that it implements the IEnumerable interface and use GetEnumerator() to get an enumerator-object which it can use to loop through the collection.

This:

// C#
foreach (Transform child in transform)
{
    // ...
}

performs actually something like this:

IEnumerator tmpEnumerator = transform.GetEnumerator();
while (tmpEnumerator.MoveNext())
{
    Transform child = (Transform)tmpEnumerator.Current;
    // ...
}

edit

To use a class "like an array" you can use an Indexer.

Indexers and additional interfaces are not supported by UnityScript(Unity's Javascript) at the moment. The only way would be to use C#

That for loop can be used with any arrays, no matter what is in there. I think AnimationStates is a hashtable, which mean you can access elements with a key, a string in that case.