Method I Wish Was There: AsUri

I never got used to the .NET Uri type. It seems like I only had to use it occasionally and even then where I really only wanted to type a string url.

public static Uri AsUri(this string uri) { return new Uri(uri); }

This way I can just add a AsUri() when I forgot that I was supposed to pass a Uri instead of a string.

webclient.DownloadFileAsync( “http://blah.com”, filename );

doesn’t compile and so I can scratch my head once again and replace it.

webclient.DownloadFileAsync( “http://blah.com”.AsUri(), filename );

I like trivial things 🙂

1 thought on “Method I Wish Was There: AsUri”

  1. Actually C# (5?) should start supporting Uri and Regex datatypes as literals.

    EcmaScript has for example the support for Regex.
    Visual Basic has for example the support for XML.

    C# should start supporting Uri’s too 🙂

    var u = http://example.com/?demo=url

Comments are closed.