Get Element Or Default from IEnumerable by index - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Get Element Or Default from IEnumerable by index

Demo Code


using System.Collections.Generic;
using System.Collections;
using System;/*from  w w  w.  jav a 2 s . com*/

public class Main{
        public static T GetElementOrDefault<T>(IEnumerable<T> collection, int at)
        {
            int idx = 0;
            foreach (T obj in collection)
            {
                if (idx == at)
                    return obj;
                idx++;
            }

            return default(T);

        }
}

Related Tutorials