Returns either the list or a new List if list is null. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:List

Description

Returns either the list or a new List if list is null.

Demo Code


using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;/*w  ww  .j  av  a 2s  . c o m*/

public class Main{
        /// <summary>
      /// Returns either the list or a new List if list is null.
      /// </summary>
      /// <typeparam name="T"></typeparam>
      /// <param name="list"></param>
      /// <returns></returns>
      public static List<T> NotNull<T>(this List<T> list)
      {
         if (list == null)
         {
            list = new List<T>();
         }
         return list;
      }
}

Related Tutorials