Click here to Skip to main content
15,880,905 members
Articles / Programming Languages / C# 4.0

Util Library and Extension Methods

Rate me:
Please Sign up or sign in to vote.
4.86/5 (49 votes)
7 May 2013CPOL2 min read 47.7K   1.6K   131   9
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.

ProjectNamespaces
DateExtensions
  1. DateTimeExtensions
NumberExtensions
  1. NumberExtensions
  2. NumberExtensions.FluentDate <linumberextensions.computation.statistics>
  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

C#
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[^].

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
QuestionQuestions and observations Pin
kornman007-May-13 9:35
kornman007-May-13 9:35 
GeneralJust one thing Pin
Thorsten Bruning7-May-13 6:51
Thorsten Bruning7-May-13 6:51 
GeneralMy vote of 5 Pin
fredatcodeproject7-May-13 1:23
professionalfredatcodeproject7-May-13 1:23 
SuggestionThese may be of interest Pin
jim lahey7-May-13 0:06
jim lahey7-May-13 0:06 
GeneralMy vote of 5 Pin
Savalia Manoj M25-Feb-13 1:31
Savalia Manoj M25-Feb-13 1:31 
GeneralGood One.. Pin
SureshGubba27-Apr-10 21:09
SureshGubba27-Apr-10 21:09 
GeneralRe: Good One.. Pin
Farhan Ejaz2-May-10 19:08
Farhan Ejaz2-May-10 19:08 
GeneralRe: Good One.. Pin
cfitzh2o2-Dec-16 9:41
cfitzh2o2-Dec-16 9:41 
GeneralBrilliant Methods Pin
Anthony Daly26-Aug-09 4:49
Anthony Daly26-Aug-09 4: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.