Get first item in a collection - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Get first item in a collection

Demo Code


using System.Text;
using System.Collections.Generic;
using System;/* www.jav a 2  s  .  com*/

public class Main{
        /// <summary>
        /// Get first item in a collection
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection"></param>
        /// <returns></returns>
        public static T First<T>(ICollection<T> collection)
        {
            foreach (T item in collection)
                return item;
            return default(T);
        }
}

Related Tutorials