Click here to Skip to main content
13,835,377 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


133 bookmarked
Posted 23 Aug 2009
Licenced CPOL

Util Library and Extension Methods

, 7 May 2013
Rate this:
Please Sign up or sign in to vote.
A simple Util library.


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.


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.

  1. DateTimeExtensions
  1. NumberExtensions
  2. NumberExtensions.FluentDate <linumberextensions.computation.statistics />
  3. NumberExtensions.Computation.Mathematical
  4. NumberExtensions.Collections
  1. StringExtensions
  2. StringExtensions.Encode
  3. StringExtensions.Validators




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.


using DateTimeExtensions;

    DateTime dateTime1 = new DateTime(2009, 1, 1);
    DateTime floor1 = new DateTime(2008, 12, 31);
    DateTime ceiling1 = new DateTime(2009, 1, 2);

    ///some logic


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

using NumberExtensions.FluentDate;

    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);
 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


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


About the Author

Farhan Ejaz
Software Developer (Senior) Schneider Electric, GTCI Bangalore
India India
Music is my passion,
Apart from programming I like to read a lot.

You may also be interested in...


Comments and Discussions

QuestionQuestions and observations Pin
kornman007-May-13 10:35
memberkornman007-May-13 10:35 
GeneralJust one thing Pin
Thorsten Bruning7-May-13 7:51
memberThorsten Bruning7-May-13 7:51 
GeneralMy vote of 5 Pin
fredatcodeproject7-May-13 2:23
professionalfredatcodeproject7-May-13 2:23 
SuggestionThese may be of interest Pin
jim lahey7-May-13 1:06
memberjim lahey7-May-13 1:06 
GeneralMy vote of 5 Pin
Savalia Manoj M25-Feb-13 2:31
memberSavalia Manoj M25-Feb-13 2:31 
GeneralGood One.. Pin
SureshGubba27-Apr-10 22:09
memberSureshGubba27-Apr-10 22:09 
GeneralRe: Good One.. Pin
Farhan Ejaz2-May-10 20:08
memberFarhan Ejaz2-May-10 20:08 
GeneralRe: Good One.. Pin
cfitzh2o2-Dec-16 10:41
membercfitzh2o2-Dec-16 10:41 
GeneralBrilliant Methods Pin
Anthony Daly26-Aug-09 5:49
memberAnthony Daly26-Aug-09 5:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190114.1 | Last Updated 7 May 2013
Article Copyright 2009 by Farhan Ejaz
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid