Explicit List Default Value - CSharp Language Basics

CSharp examples for Language Basics:default

Description

Explicit List Default Value

Demo Code

using System;/*from  w w w. j  a  v  a 2  s . co m*/
using System.Collections.Generic;
using System.Text;
class ExplicitListDefaultValue
{
   static void Main()
   {
      var list = new List<int> { 5, 2, 1 };
      Console.WriteLine(GetValueOrDefault(list, 1));
      Console.WriteLine(GetValueOrDefault(list, 10));
   }
   static T GetValueOrDefault<T>(IList<T> list, int index)
   {
      return index >= 0 && index < list.Count ? list[index] : default(T);
   }
}

Result


Related Tutorials