REQUEST A DEMO

Extension Methods I Use A Lot

C# extensions have been around for some time and it was a fad for a while to post your favorites. I am following way behind the extension party bus with this post of my favorite extensions. Most of these are poached from Chad and Jeremy and Josh with a few sadly only one of my own mixed in.

Below is the Basic Extensions class that is common to a lot of our Dovetail projects. I am not a big believer in class libraries for simple little things like these as extensions that are useful on one project will be useless on another.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;

namespace Dovetail.Commons
{
    public static class BasicExtensions
    {
        /// <summary>
        /// Write string to the console. 
        /// </summary>
        public static void WriteToConsole(this string stringValue)
        {
            Console.WriteLine(stringValue);
        }

        /// <summary>
        /// Fills the format string with the provided arguments
        /// </summary>
        public static string ToFormat(this string format, params object[] args)
        {
            return String.Format(format, args);
        }

        public static string ToJson(this object objectToSerialize)
        {
            return new JavaScriptSerializer().Serialize(objectToSerialize);
        }

        public static T DeserializeJSON<T>(this string json) where T : class
        {
            return new JavaScriptSerializer().Deserialize<T>(json);
        }

        /// <summary>
        /// Returns true if the value being tested is not null and not an empty string
        /// </summary>
        public static bool IsNotEmpty(this string stringValue)
        {
            return !String.IsNullOrEmpty(stringValue);
        }

        /// <summary>
        /// Returns true if the value being tested is null or an empty string
        /// </summary>
        public static bool IsEmpty(this string stringValue)
        {
            return String.IsNullOrEmpty(stringValue);
        }

        /// <summary>
        /// Returns true if the value being tested is null or contains no items
        /// </summary>
        public static bool IsEmpty<T>(this IEnumerable<T> enumerable)
        {
            return (enumerable == null || enumerable.Count() < 1);
        }

        /// <summary>
        /// Performs an action with a counter for each item in a sequence and provides
        /// </summary>
        public static IEnumerable<T> Each<T>(this IEnumerable<T> values, Action<T, int> eachAction)
        {
            var index = 0;
            foreach (var item in values)
            {
                eachAction(item, index++);
            }

            return values;
        }

        /// <summary>
        /// Performs an action for each item in a sequence. Good for one-line foreach statements.
        /// </summary>
        public static IEnumerable<T> Each<T>(this IEnumerable<T> values, Action<T> eachAction)
        {
            foreach (var item in values)
            {
                eachAction(item);
            }

            return values;
        }

        /// <summary>
        /// Performs an action for each item in a sequence. Good for one-line foreach statements.
        /// </summary>
        public static IEnumerable Each(this IEnumerable values, Action<object> eachAction)
        {
            foreach (var item in values)
            {
                eachAction(item);
            }

            return values;
        }
    }
}

Enjoy and as always your mileage may vary. Don’t over do it with the extension methods. They are handy but can get in the way if overused. For example you might want think twice before adding an extension to System.Object that will get seen by every type in the system.