c# - How to get the first element of IEnumerable (non-generic)? -


this question has answer here:

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:

  1. if know objects in enumerable of same type, can cast generic ienumerable<yourtype>. in worst case can use object:

    object first = enumerable.cast<object>().first(); 
  2. or can use enumerator, make 1 step , take current element:

    ienumerator enumerator = enumerable.getenumerator(); enumerator.movenext(); object first = enumerator.current; 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -