DovetailConnect Blog

Extension Methods I Use A Lot

Kevin Miller September 23, 2009

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.

share

Tags:

Comments

Re: Extension Methods I Use A Lot

Good idea Jordan thank you for the suggestion I'll give it a try.

Re: Extension Methods I Use A Lot

@fiend I like your suggestion and indeed that sounds like the way to go. @jordan For my project I am only using the Each extension as a foreach statement shorthand. I decided to remove the return value we never use it and I believe it to cause confusion. I agree that when chaining Each with other actions on enumerables that deferring them is preferred.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.