How to count IEnumerator<T> without compiler warnings?

How do I count an IEnumerable without generating this warning?

IEnumerable<Vector3> someEnumerable; // get this somehow.

int count = 0;
foreach (Vector3 v in someEnumerable)
    count++;

Debug.Log("There are " + count + " Vector3s in someEnumerable.");

This gives me an warning: [quote]
“The variable `v’ is assigned but its value is never used”
[/quote]How should I write this so there is no warning? Seems so simple!

Just use

var count = someEnumerable.Count();

.Count() requires LINQ which I would like to avoid. I’ve had problems with LINQ on iOS due to AOT compile limitations. It’s not all LINQ functions, but I’ve kinda learned to avoid it.

IEnumerable<Vector3> someEnumerable; // get this somehow.
var enumerator = someEnumerable.GetEnumerator();
int count = 0;
while (enumerator.MoveNext()) {
    count++;
}

Debug.Log("There are " + count + " Vector3s in someEnumerable.");

An alternative is to do what you’re already doing, and use #pragma warning to disable the warnings.

1 Like