| Subscribe via RSS

C# 3 Conversion Extension Method Library 1.0

September 19th, 2007 Posted in C#

If you have had a chance to muck around with the new language features of C# 3 in the latest beta of Orcas, I’m sure you will be as much of a fan of extension methods as I am. The ability to package little ‘lumps’ of functionality and add them onto objects that I use every day is a lot of fun.

I use the .ToString() method habitually so I thought I’d build a little library to perform some other .ToWhatever methods on some of the basic datatypes. So for example, the String datatype has .ToBool, .ToInt32, . ToDecimal etc. extended into it. The int32 datatype has .ToChar, .ToFloat etc. etc. It’s a nice little utility class that I hope you will find of some use. Download it and have a little dig around to see what the library is capable of.

Also, I’ve added in a couple of string manipulations as extensions too.

To reverse a string we have string.Reverse, to proper case a string we have string.ProperCase ( so for example, “jon davies” becomes “Jon Davies” ). I’ve also added string.Left and string.Right extensions, just because VB has them and C# doesn’t!! string.Left(3) returns the first 3 characters in a string, string.Right(3) returns the last 3.

Anyway, it was a lot of fun writing this - I’m going to keep adding new useful extensions to the library, so don’t forget to bookmark this site and keep an eye out for updates.

Edit: I’ve changed the name of the article thanks to the advice of posters on DotNetKicks and James to be 3.0 centric not 3.5. Apparently there is a bit of confusion as to the naming of the new updates, so it’s the .NET framework 3.5 and C# 3. Thanks for the tip off!

[download#1#image]
kick it on DotNetKicks.com

10 Responses to “C# 3 Conversion Extension Method Library 1.0”

  1. James Says:

    Hey mate. FYI it’s C# 3.0 and .Net 3.5, not C# 3.5

    Microsofts naming leads a lot to be desired at times…


  2. jdee Says:

    Thanks James, see the edit to the article above!


  3. Jay R. Wren Says:

    How about including a license, even if it is public domain or creative commons or something.

    Also, AFAICT you can change your using block to just “using System;” and get rid of all the other using statements.


  4. jdee Says:

    Hi Jay, Yep, i’ll add a CC license on version 1.1 due later this week. Also, I’ll take your advice and dump all the unused ‘using’ statements too. Thanks for the heads up!


  5. Knoton Says:

    Hi Jay, I appreciate your extension library. Really like extensions, a great feature :-)
    Something I often do is to check if something is null, eg not initialized…

    Below extension appears on all types that inherits from object.

    public static System.Boolean IsNull(this System.Object obj)
    {
    return obj == null;
    }


  6. Jeff Says:

    Along the lines of what Knoton said, all of these methods that are wrappers for Convert class methods can be done using ‘object’ rather than a set of conversions for each type and will therefore be availabe to all types. Of course, by using type-specific overloads you may be avoiding a minor performance penalty that may come with using ‘object’. Either way, nice class. Extension methods are one of my favorite new features.

    Additionally, extension methods can be called on variables that are null, which gives us some interesting capabilities. Instead of doing ‘if( string.IsNullOrEmpty(var))’ you can do ‘if(var.IsNullOrEmpty)’. Doesn’t really do anything different but, to me, it feels a little more natural.

    Anyway, good work and thanks for the example.


  7. joaomgcd Says:

    Instead of having all those little conversion methods you could make use of generics and have:

    public static T To(this IConvertible obj)
    {
    return (T)Convert.ChangeType(obj, typeof(T));
    }

    This way, strings and all other classes that inherit from IConvertible would have the ability to be converted to other compatible types.


  8. Jonathan Says:

    Thanks for the script Jon - very nice use of extensions.

    One quirk - in your ProperCase routine, I assume you mean this item (line 355):
    i.Substring(1)
    to be something like:
    i.Substring(1).ToLower()

    Without doing so, it only uppercases the first letter, without lowercasing the rest.

    It might also be nice to have a title case option which ignores (or lcases) some common prepositions/conjunctions/internal articles. Perhaps just based on a user-defined array. So the output could look something like:

    “This Application is Very Cool”


  9. Alvaro Says:

    joaomgcd:

    I like your generic method, except for one very important thing: it prevents converting an “object” type to something else. I can fix it by changing from “IConvertible” to “object”, but then all types will get it, including ones that aren’t convertible. So there’s no perfect solution.

    I wish C# had a way to specify that a parameter must of a specific type and only that type — no derived types allowed. That would allow me to overload the above method for values of type “object” only.

    Cheers!


  10. Jason Says:

    Did something break with the download link. All I see is a [download#1#image]


  • Cloud

    Ajax AmpliFeeder ASP.NET Blabnote C# Cloud Design EntityFramework Framework JavaScript JQuery LINQ Live Mesh MicroBlog Mix07 Productivity Rails Remix08 SDS Silverlight2 SQL SSDS TDD Telecommunications Uncategorized VOIP WCF Web 2.0 Webcast


  • @jonpauldavies