Returns the index element of the specified IList. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Returns the index element of the specified IList.

Demo Code

/*//from w  w w  . j  a  va2  s  .  c  o  m
    Copyright (C) 2007-2017 Team MediaPortal
    http://www.team-mediaportal.com

    This file is part of MediaPortal 2

    MediaPortal 2 is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MediaPortal 2 is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MediaPortal 2. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;

public class Main{
        /// <summary>
    /// Returns the indexth element of the specified <paramref name="list"/>. If the list is null or empty or if the index
    /// is outside the list's bounds, <c>null</c> will be returned.
    /// </summary>
    /// <typeparam name="T">Type of the list's elements.</typeparam>
    /// <param name="list">The list to access.</param>
    /// <param name="index">The index of the list to access.</param>
    /// <returns><c>list[index]</c> or <c>null</c>.</returns>
    public static T SafeGet<T>(IList<T> list, int index) where T : class
    {
      if (list == null || list.Count == 0 || index < 0 || index >= list.Count)
        return null;
      return list[index];
    }
}

Related Tutorials