José F. Romaniello

Las aventuras y desventuras de un codificador.

String extensions

public static class StringExtensions
{
    public static string F(this string text, params object[] args)
    {
        return string.Format(text, args);
    }
}

This extension method is very clear and doesn't need explanation, usage:

"Hello, my name is {0}".F("Jose")

Int extensions

public static class IntExtensions
{
    public static IEnumerable<int> To(this int start, int end)
    {
        return Enumerable.Range(start, end);
    }
}

1.To(10) will returns an IEnumerable with elements from 1 to 10

Cast extension

public static class CastsExtensions
{
    public static T To<T>(this object o)
    {
        return (T)o;
    }

    public static T As<T>(this object o) where T : class
    {
        return o as T;
    }
}

This is self descriptive too. It allows you to cast and access a property with a more cleaner syntax: someObject.To<IDataErrorInfo>().Errors

Datetime extensions

public static class DateTimeExtensions
{
    public static DateTime FromNow(this TimeSpan self)
    {
        return DateTime.Now.Add(self);
    }
    public static DateTime From(this TimeSpan self, DateTime dateTime)
    {
        return dateTime.Add(self);
    }
    public static TimeSpan Days(this int days)
    {
        return TimeSpan.FromDays(days);
    }
}

I really like this extension method:

10.Days().FromNow()
    .Should().Be.EqualTo(DateTime.Today.AddDays(10));

10.Days().From(new DateTime(2009, 10, 10))
    .Should().Be.EqualTo(new DateTime(2009, 10, 20));

Enumerable extensions

public static class EnumerableExtensionsMethods
{
    public static void ForEach<T>(
            this IEnumerable<T> enumerable, 
            Action<T> action)
    {
        foreach (T item in enumerable)
        {
            action(item);
        }
    }
}

But, I already explained this in this post.

| More

6 comentarios:

Gotta love extensions. I'm going to add a <span style="">FromUtcNow to the Datetime extensions since I use that much more often than Now.</span>

I _really_ like the DateTime extensions expresivity.
I found easier to read string.Format(...) than the string extension here, but i understand it is easier (and more criptic) to write what you did
i don't like so muchj 1.To(20) i cannot deduce from this that it should do something...

José F. Romaniello dijo...

Thank you for your comment.
Yes the string and integer function are usefull for me, but maybe the name
is not proper.
I found the format function more object-action oriented than
string.Format(..). I have an string and I want to apply this format.
Calling string.format remind me VB6; left right mid format, all the string
functions are used:
left(someString, 2).. I want someString.Left(2)!

José F. Romaniello dijo...

<span style="color: #808080;">Perfect! </span>
<span style="color: #808080;">Thank you for your comment! </span>
<span style="color: #808080;">Maybe, I will update the post  later.</span>

Darius Damalakas dijo...

So simple and so nice! I have not though myself of writing these :)   
Definetely will use it!!

José F. Romaniello dijo...

<span style="color: #808080;">Thank you. What do you think if I open a new open source project for this kind of extensions methods?</span>
The main gol will be "Syntax sugar for C#"? I'm open to suggestions.

Publicar un comentario en la entrada