Saturday, December 14, 2013

Generic TryParse handler for generic .Net Lists



Following C# code sample is a Generic TryParse handler for generic value type 
lists in .Net.  Any character delimited list can be split and use safe TryParse against split list 
to ensure you have valid data.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GenericTryParsehandler
{
    /// <summary>
    /// Generic TryParsehandler to split and parse generic value type lists.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="value"></param>
    /// <param name="result"></param>
    /// <returns></returns>
    public delegate bool TryParseHandler<T>(string value, out T result);
    class Program
    {
        static void Main(string[] args)
        {
            var semiColonDelimitedDateInput = "01/01/2001;2/02/2012;05/31/2013;05/05/1992";
            var commaDelimitedIntInput = "3,4,5,6";
            var intresult = commaDelimitedIntInput.TryParseList<int>(new[] { ',' }, int.TryParse);
            var dateresult = semiColonDelimitedDateInput.TryParseList<DateTime>(new[] { ';' }, DateTime.TryParse);
            
            if (dateresult.Count > 0)
            {
                foreach (var element in dateresult)
                {
                    Console.WriteLine(element);
                }
            }
            else
            {
                Console.WriteLine("Your semiColonDelimitedDateInput cannot be parsed.");
            }
            Console.WriteLine("*******************************************************************");
            if (intresult.Count > 0)
            {
                foreach (var element in intresult)
                {
                    Console.WriteLine(element);
                }
            }
            else
            {
                Console.WriteLine("Your commaDelimitedIntInput cannot be parsed.");
            }
            Console.ReadLine();
        }
    }


    /// <summary>
    /// 
    /// </summary>

    public static class ExtensionMethods
{
        /// <summary>
        /// Generic handler to split and TryParse each item in the list. Handler supports value types.. 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="separator"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
    public static List<T> TryParseList<T>(this string source, char[] separator, TryParseHandler<T> handler)
        where T : struct

    {
        var splitList = new List<T>();
        if (string.IsNullOrEmpty(source)) return splitList;

        var parsedString = source.Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList();

        parsedString.ForEach(x =>
        {
            T result;
            if (handler(x, out result))
            {
                splitList.Add(result);
            }
        });

        return splitList.Count == parsedString.Count ? splitList : new List<T>();
    }
}
}