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 |
DateTimeExtensions
|
NumberExtensions |
NumberExtensions
NumberExtensions.FluentDate
NumberExtensions.Computation.Mathematical
NumberExtensions.Collections
|
StringExtensions |
StringExtensions
StringExtensions.Encode
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.
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))
{
}
}
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...
- Raymond Glover for his TimeSpan Articulator
- Frank-Leonardo Quednau for his Fluent DateTime library
- Chad Finsterwald for his String Library