Click here to Skip to main content
15,891,762 members
Articles / Programming Languages / C#

Having fun with C# 3.0 extensions

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
7 Dec 2008CPOL3 min read 30.1K   74   16  
Implementing an extension utility class.
namespace ExtensionExampleApp.Utility
{
    public static class Extensions
    {
        /// <summary>
        /// Checks if string is null, empty or consists only of spaces (in which case returns false)
        /// </summary>
        /// <param name="value"></param>
        /// <returns>true if string is none of the above</returns>
        public static bool HasValue(this string text)
        {
            return (text != null && text.Trim() != string.Empty);        
        }


        public static string AddExclamationMark(this string text)
        {
            return text + "!";
        }

        public static string AddAditionalText(this string text, string add)
        {
            return text + add;
        }
    }

    public static class StringHelper
    {
        //static method for C# 2.0
        //identical to Exentensions.HasValue except for the keyword 'this'
        public static bool HasValue(string text)
        {
            return (text != null && text.Trim() != string.Empty);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions