c# - How to get the first element of IEnumerable (non-generic)? -
this question has answer here:
- how first element of ienumerable 5 answers
if have generic ienumerable<int>
. can apply tolist()
or toarray()
or firstordefault()
it. how apply these methods non-generic ienumerable
?
you have couple of options:
if know objects in enumerable of same type, can cast generic
ienumerable<yourtype>
. in worst case can useobject
:object first = enumerable.cast<object>().first();
or can use enumerator, make 1 step , take current element:
ienumerator enumerator = enumerable.getenumerator(); enumerator.movenext(); object first = enumerator.current;
Comments
Post a Comment