Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Util Library and Extension Methods

0.00/5 (No votes)
7 May 2013 1  
A simple Util library.

Introduction

This is my first article on CodeProject. Although this is entirely not an original thought and work, I still wanted to share this with the community.

I was trying to collect various utility functions that we often need and most of the time we turn to Google for code and reuse it. Sometimes it becomes tedious to browse various articles to bounce on the exact code. My objective of this project was to create a reusable utility library with most possible functions we need across.

Background

While working on LINQ in one of my projects, I was quite fascinated by the use of extension methods in the .NET Framework.

Definition of Extension Methods on MSDN[^]:

“Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type.”

Using the Code

The solution consists of three main projects.

Project Namespaces
DateExtensions
  1. DateTimeExtensions
NumberExtensions
  1. NumberExtensions
  2. NumberExtensions.FluentDate
  3. NumberExtensions.Computation.Mathematical
  4. NumberExtensions.Collections
StringExtensions
  1. StringExtensions
  2. StringExtensions.Encode
  3. StringExtensions.Validators

Utilities/DateTime.png

Utilities/Number.png

Utilities/string.png

To enable extension methods for a particular type, just add a using directive for the namespace in which the methods are defined. If you feel that extension methods are not for you, you can reuse the code at your own will.

DateExtensions

using DateTimeExtensions;

[Test]
public void TEST_DATE_WITHIN_RANGE()
{
    DateTime dateTime1 = new DateTime(2009, 1, 1);
    DateTime floor1 = new DateTime(2008, 12, 31);
    DateTime ceiling1 = new DateTime(2009, 1, 2);

    if(dateTime1.IsWithinRange(floor1,ceiling1,true))
    {
    ///some logic
    }            
}

NumberExtensions

The most interesting is the NumberExtensions.FluentDate. Check the original post from Frank-Leonardo Quednau[^].

using NumberExtensions.FluentDate;

[Test]
public void TEST_FLUENT_DATETIME_ONE()
{
    DateTime dateTime1 = DateTime.Now;
    DateTime value1 = 1.Years().Ago;
    DateTime value2 = 1.Weeks().FromNow;
    DateTime value3 = 1.Days().FromNow;
    DateTime value4 = 2.Minutes().Ago;


    DateTime result1 = dateTime1.AddYears(-1);
    DateTime result2 = dateTime1.AddDays(7);
    DateTime result3 = dateTime1.AddDays(1);
    DateTime result4 = dateTime1.AddMinutes(-2);

    Assert.AreEqual(result1, value1);
    Assert.AreEqual(result2, value2);
    Assert.AreEqual(result3, value3);
    Assert.AreEqual(result4, value4);
}
[Test]
 public void TEST_AGE_AT()
{
    DateTime dateOfBirth1 = new DateTime(1984, 3, 21);
    DateTime referenceDate1 = new DateTime(1985, 1, 1);
    Age result1 = new Age(0, 9, 11);
    string ageInString1 = "9 Months, 11 Days";

    Assert.AreEqual(result1.Years, dateOfBirth1.AgeAt(referenceDate1).Years);
    Assert.AreEqual(result1.Months, dateOfBirth1.AgeAt(referenceDate1).Months);
    Assert.AreEqual(result1.Days, dateOfBirth1.AgeAt(referenceDate1).Days);
    Assert.AreEqual(ageInString1, result1.ToString());
}

I have included an NUnit project where you can explore usages of most of the functions defined.

Points of Interest

Some of the functions are not implemented and tested exhaustively. I would appreciate suggestions on various other functions that can be used to enhance the library. I would try to update the library frequently.

Words of Appreciation For...

  1. Raymond Glover for his TimeSpan Articulator
  2. Frank-Leonardo Quednau for his Fluent DateTime library
  3. Chad Finsterwald for his String Library

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here