These ones are pretty simple in implementation but go a long way to cleaning up your code. In some cases, the slight performance overhead may not be acceptable but I haven’t run into any real-world problems with them. Check out the usage below.

int x = 3;

// is x either 1, 3, or 5?
if (x.In(1, 3, 5)) {
    // yes!
}

// is x between 1 and 5?
if (x.Between(1, 5)) {
    // yes!
}

The In method works with any kind of object and uses the default EqualityComparer<T> implementation for the type. But there are overloads that specify common types such as int, byte, long, etc. These overloads will perform much better than the generic overload. There is also an overload for String that takes a comparer so that you can specify case-insensitive matching as needed.

The Between method works on any type implementing IComparable. It also has overloads for commonly used types to get better performance than using the default IComparable implementation.

The code for both extension methods is shown below.

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

namespace Einstein
{

    /// <summary>
    /// Extension methods for <see cref="T:IComparable"/> types.
    /// </summary>
    public static class ComparableExtensions
    {

        #region Methods

        #region In

        /// <summary>
        /// Determines whether this value exists in the <paramref name="set"/> passed.
        /// </summary>
        /// <typeparam name="T">The type of the value to compare and the values in the set.</typeparam>
        /// <param name="value">The value.</param>
        /// <param name="comparer">The comparer to use.</param>
        /// <param name="set">The set of values to determine if <paramref name="value"/> is in.</param>
        /// <returns>True if <paramref name="value"/> exists in the <paramref name="set"/> otherwise, false.</returns>
        public static bool In<T>( this T value, IEqualityComparer<T> comparer, params T[] set )
        {
            
            // Use default comparer
            if ( comparer == null ) {
                comparer = EqualityComparer<T>.Default;
            }   // if

            // Constant false
            if ( set.Length == 0 ) {
                return false;
            }   // if

            for ( int i = 0; i < set.Length; i++ ) {
                if ( comparer.Equals( value, set[i] ) ) {
                    return true;
                }   // if
            }   // for

            return false;

        }

        /// <summary>
        /// Determines whether this value exists in the <paramref name="set"/> passed.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="set">The set of values to determine if <paramref name="value"/> is in.</param>
        /// <returns>True if <paramref name="value"/> exists in the <paramref name="set"/> otherwise, false.</returns>
        public static bool In( this byte value, params byte[] set )
        {
            for ( int i = 0; i < set.Length; i++ ) {
                if ( value == set[i] ) {
                    return true;
                }   // if
            }   // for
            return false;
        }

        /// <summary>
        /// Determines whether this value exists in the <paramref name="set"/> passed.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="set">The set of values to determine if <paramref name="value"/> is in.</param>
        /// <returns>True if <paramref name="value"/> exists in the <paramref name="set"/> otherwise, false.</returns>
        public static bool In( this short value, params short[] set )
        {
            for ( int i = 0; i < set.Length; i++ ) {
                if ( value == set[i] ) {
                    return true;
                }   // if
            }   // for
            return false;
        }

        /// <summary>
        /// Determines whether this value exists in the <paramref name="set"/> passed.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="set">The set of values to determine if <paramref name="value"/> is in.</param>
        /// <returns>True if <paramref name="value"/> exists in the <paramref name="set"/> otherwise, false.</returns>
        public static bool In( this int value, params int[] set )
        {
            for ( int i = 0; i < set.Length; i++ ) {
                if ( value == set[i] ) {
                    return true;
                }   // if
            }   // for
            return false;
        }

        /// <summary>
        /// Determines whether this value exists in the <paramref name="set"/> passed.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="set">The set of values to determine if <paramref name="value"/> is in.</param>
        /// <returns>True if <paramref name="value"/> exists in the <paramref name="set"/> otherwise, false.</returns>
        public static bool In( this long value, params long[] set )
        {
            for ( int i = 0; i < set.Length; i++ ) {
                if ( value == set[i] ) {
                    return true;
                }   // if
            }   // for
            return false;
        }

        /// <summary>
        /// Determines whether this value exists in the <paramref name="set"/> passed.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="set">The set of values to determine if <paramref name="value"/> is in.</param>
        /// <returns>True if <paramref name="value"/> exists in the <paramref name="set"/> otherwise, false.</returns>
        public static bool In( this string value, params string[] set )
        {
            return In<string>( value, StringComparer.OrdinalIgnoreCase, set );
        }

        /// <summary>
        /// Determines whether this value exists in the <paramref name="set"/> passed.
        /// </summary>
        /// <typeparam name="T">The type of the value to compare and the values in the set.</typeparam>
        /// <param name="value">The value.</param>
        /// <param name="set">The set of values to determine if <paramref name="value"/> is in.</param>
        /// <returns>True if <paramref name="value"/> exists in the <paramref name="set"/> otherwise, false.</returns>
        public static bool In<T>( this T value, params T[] set )
        {
            return In<T>( value, EqualityComparer<T>.Default, set );
        }

        #endregion

        #region Between

        /// <summary>
        /// Determines if the <paramref name="value"/> is between <paramref name="minValueInclusive"/> and
        /// <paramref name="maxValueInclusive"/>.
        /// </summary>
        /// <typeparam name="T">The type of values to compare.</typeparam>
        /// <param name="value">The value to check.</param>
        /// <param name="minValueInclusive">The min value inclusive.</param>
        /// <param name="maxValueInclusive">The max value inclusive.</param>
        /// <returns>True if <paramref name="value"/> is between <paramref name="minValueInclusive"/> and 
        /// <paramref name="maxValueInclusive"/>, otherwise false.</returns>
        public static bool Between<T>( this T value, T minValueInclusive, T maxValueInclusive ) where T : IComparable
        {

            ParameterValidation.ThrowIfNull( value, "value" );

            int o1 = value.CompareTo( minValueInclusive );
            int o2 = value.CompareTo( maxValueInclusive );

            return ( o1 >= 0 && o2 <= 0 ) || ( o1 <= 0 && o2 >= 0 );

        }

        /// <summary>
        /// Determines if the <paramref name="value"/> is between <paramref name="minValueInclusive"/> and
        /// <paramref name="maxValueInclusive"/>.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="minValueInclusive">The min value inclusive.</param>
        /// <param name="maxValueInclusive">The max value inclusive.</param>
        /// <returns>True if <paramref name="value"/> is between <paramref name="minValueInclusive"/> and 
        /// <paramref name="maxValueInclusive"/>, otherwise false.</returns>
        public static bool Between( this byte value, byte minValueInclusive, byte maxValueInclusive )
        {
            if ( minValueInclusive <= maxValueInclusive ) {
                return ( value >= minValueInclusive ) && ( value <= maxValueInclusive );
            }   // if
            else {
                return ( value >= maxValueInclusive ) && ( value <= minValueInclusive );
            }   // else
        }

        /// <summary>
        /// Determines if the <paramref name="value"/> is between <paramref name="minValueInclusive"/> and
        /// <paramref name="maxValueInclusive"/>.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="minValueInclusive">The min value inclusive.</param>
        /// <param name="maxValueInclusive">The max value inclusive.</param>
        /// <returns>True if <paramref name="value"/> is between <paramref name="minValueInclusive"/> and 
        /// <paramref name="maxValueInclusive"/>, otherwise false.</returns>
        public static bool Between( this short value, short minValueInclusive, short maxValueInclusive )
        {
            if ( minValueInclusive <= maxValueInclusive ) {
                return ( value >= minValueInclusive ) && ( value <= maxValueInclusive );
            }   // if
            else {
                return ( value >= maxValueInclusive ) && ( value <= minValueInclusive );
            }   // else
        }

        /// <summary>
        /// Determines if the <paramref name="value"/> is between <paramref name="minValueInclusive"/> and
        /// <paramref name="maxValueInclusive"/>.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="minValueInclusive">The min value inclusive.</param>
        /// <param name="maxValueInclusive">The max value inclusive.</param>
        /// <returns>True if <paramref name="value"/> is between <paramref name="minValueInclusive"/> and 
        /// <paramref name="maxValueInclusive"/>, otherwise false.</returns>
        public static bool Between( this int value, int minValueInclusive, int maxValueInclusive )
        {
            if ( minValueInclusive <= maxValueInclusive ) {
                return ( value >= minValueInclusive ) && ( value <= maxValueInclusive );
            }   // if
            else {
                return ( value >= maxValueInclusive ) && ( value <= minValueInclusive );
            }   // else
        }

        /// <summary>
        /// Determines if the <paramref name="value"/> is between <paramref name="minValueInclusive"/> and
        /// <paramref name="maxValueInclusive"/>.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="minValueInclusive">The min value inclusive.</param>
        /// <param name="maxValueInclusive">The max value inclusive.</param>
        /// <returns>True if <paramref name="value"/> is between <paramref name="minValueInclusive"/> and 
        /// <paramref name="maxValueInclusive"/>, otherwise false.</returns>
        public static bool Between( this long value, long minValueInclusive, long maxValueInclusive )
        {
            if ( minValueInclusive <= maxValueInclusive ) {
                return ( value >= minValueInclusive ) && ( value <= maxValueInclusive );
            }   // if
            else {
                return ( value >= maxValueInclusive ) && ( value <= minValueInclusive );
            }   // else
        }

        /// <summary>
        /// Determines if the <paramref name="value"/> is between <paramref name="minValueInclusive"/> and
        /// <paramref name="maxValueInclusive"/>.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="minValueInclusive">The min value inclusive.</param>
        /// <param name="maxValueInclusive">The max value inclusive.</param>
        /// <returns>True if <paramref name="value"/> is between <paramref name="minValueInclusive"/> and 
        /// <paramref name="maxValueInclusive"/>, otherwise false.</returns>
        public static bool Between( this decimal value, decimal minValueInclusive, decimal maxValueInclusive )
        {
            if ( minValueInclusive <= maxValueInclusive ) {
                return ( value >= minValueInclusive ) && ( value <= maxValueInclusive );
            }   // if
            else {
                return ( value >= maxValueInclusive ) && ( value <= minValueInclusive );
            }   // else
        }

        /// <summary>
        /// Determines if the <paramref name="value"/> is between <paramref name="minValueInclusive"/> and
        /// <paramref name="maxValueInclusive"/>.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="minValueInclusive">The min value inclusive.</param>
        /// <param name="maxValueInclusive">The max value inclusive.</param>
        /// <returns>True if <paramref name="value"/> is between <paramref name="minValueInclusive"/> and 
        /// <paramref name="maxValueInclusive"/>, otherwise false.</returns>
        public static bool Between( this float value, float minValueInclusive, float maxValueInclusive )
        {
            if ( minValueInclusive <= maxValueInclusive ) {
                return ( value >= minValueInclusive ) && ( value <= maxValueInclusive );
            }   // if
            else {
                return ( value >= maxValueInclusive ) && ( value <= minValueInclusive );
            }   // else
        }

        /// <summary>
        /// Determines if the <paramref name="value"/> is between <paramref name="minValueInclusive"/> and
        /// <paramref name="maxValueInclusive"/>.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="minValueInclusive">The min value inclusive.</param>
        /// <param name="maxValueInclusive">The max value inclusive.</param>
        /// <returns>True if <paramref name="value"/> is between <paramref name="minValueInclusive"/> and 
        /// <paramref name="maxValueInclusive"/>, otherwise false.</returns>
        public static bool Between( this double value, double minValueInclusive, double maxValueInclusive )
        {
            if ( minValueInclusive <= maxValueInclusive ) {
                return ( value >= minValueInclusive ) && ( value <= maxValueInclusive );
            }   // if
            else {
                return ( value >= maxValueInclusive ) && ( value <= minValueInclusive );
            }   // else
        }

        /// <summary>
        /// Determines if the <paramref name="value"/> is between <paramref name="minValueInclusive"/> and
        /// <paramref name="maxValueInclusive"/>.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="minValueInclusive">The min value inclusive.</param>
        /// <param name="maxValueInclusive">The max value inclusive.</param>
        /// <returns>True if <paramref name="value"/> is between <paramref name="minValueInclusive"/> and 
        /// <paramref name="maxValueInclusive"/>, otherwise false.</returns>
        public static bool Between( this DateTime value, DateTime minValueInclusive, DateTime maxValueInclusive )
        {
            if ( minValueInclusive <= maxValueInclusive ) {
                return ( value >= minValueInclusive ) && ( value <= maxValueInclusive );
            }   // if
            else {
                return ( value >= maxValueInclusive ) && ( value <= minValueInclusive );
            }   // else
        }

        /// <summary>
        /// Determines if the <paramref name="value"/> is between <paramref name="minValueInclusive"/> and
        /// <paramref name="maxValueInclusive"/>.
        /// </summary>
        /// <param name="value">The value to check.</param>
        /// <param name="minValueInclusive">The min value inclusive.</param>
        /// <param name="maxValueInclusive">The max value inclusive.</param>
        /// <returns>True if <paramref name="value"/> is between <paramref name="minValueInclusive"/> and 
        /// <paramref name="maxValueInclusive"/>, otherwise false.</returns>
        public static bool Between( this TimeSpan value, TimeSpan minValueInclusive, TimeSpan maxValueInclusive )
        {
            if ( minValueInclusive <= maxValueInclusive ) {
                return ( value >= minValueInclusive ) && ( value <= maxValueInclusive );
            }   // if
            else {
                return ( value >= maxValueInclusive ) && ( value <= minValueInclusive );
            }   // else
        }
        
        #endregion

        #endregion

    }   // class

}   // namespace

9-Year-Old is World's Youngest Microsoft Certified Systems Engineer This is pretty impressive. When I was 9 I was still trying to figure out how I could get Guns N’ Roses to come to my birthday party. Of course the cynics will probably claim it is a reflection of the MCSE certification, but how can you not be impressed?

9-Year-Old is World’s Youngest Microsoft Certified Systems Engineer

via Doug Finke

Note sure how Facebook gets off the hook on this one. This is entirely a *Facebook* problem, not AT&T. If Facebook wasn’t developed by morons with no regard for privacy and security, this bug wouldn’t be possible.

AT&T fixes bug that logged users into random Facebook accounts

Okay, so we were under the impression that Facebook login credentials were a locally-managed affair, but it looks like almost anything can break when AT&T’s involved — according to CNET, the carrier just fixed "several problems" that had users logging into the wrong Facebook account from their phones.

Ed Bott has thrown down the gauntlet when it comes to companies that still use Internet Explorer 6. Users stuck on IE6 are one of the main reasons IE still has such a bad reputation when it comes to security even though Firefox and Safari were found to be far more vulnerable than Internet Explorer in 2009. And according to Ed, allowing people to use IE6 is akin to IT-malpractice.

Any IT professional who is still allowing IE6 to be used in a corporate setting is guilty of malpractice. Think that judgment is too harsh? Ask the security experts at Google, Adobe, and dozens of other large corporations that are cleaning up the mess from a wave of targeted attacks that allowed source code and confidential data to fall into the hands of well-organized intruders. The entry point? According to Microsoft, it’s IE6.

First, upgrade to Internet Explorer 8 if you haven’t already.

Then, read more: It’s time to stop using IE6

There are all sorts of official ways of doing this implementing various IE hosting interfaces in order to handle the presentation of the UI. But if you’re trying to automate a web page and a pesky window.alert is blocking your progress, the following code will supress it for the current page.

/// <summary>
/// Handles the Navigated event of the browser control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="T:WebBrowserNavigatedEventArgs"/> instance containing the 
/// event data.</param>
private void browser_Navigated( object sender, WebBrowserNavigatedEventArgs e )
{

    if ( browser.Document != null ) {

        // kills messagebox functionality by disabling the "window.alert()" function
        object window = browser.Document.Window.DomWindow;
        if ( window != null ) {
            Type windowType = window.GetType();
            BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Instance;
            string[] args = { "window.alert=function() {};", "JScript" };
            windowType.InvokeMember( "[DispID=1165]", flags, null, window, args );
        }   // if

    }   // if

}