Click here to Skip to main content
11,496,146 members (461 online)
Click here to Skip to main content

C# Universal String Parsing Utility

, 3 Aug 2012 CPOL 7.7K 5
A simple utility to help convert strings into other types
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

The Problem

Parsing strings into other objects using tryParse() is a pain, and results in ugly code. I was sick and tired of seeing this kind of thing all over my projects:

var someStringThatShouldBeAnInt = "10";
int i = 0;
if(int.TryParse(someStringThatShouldBeAnInt, out i))
    //Continue on with things
    //Complain about it

The Desired Solution

Abstract this ugliness and replace it with something like this: 

var someStringThatShouldBeAnInt = "10";
var i = someStringThatShouldBeAnInt.Parse<int>();

The How

Seems like if I could dynamically call the tryParse() using Reflection I could write this once for every type that has the tryParse() available to it.

public static T Parse<T>(this string thingToParse)
    var retType = typeof(T);
    var tParse = retType.GetMethod("TryParse",
                                   BindingFlags.Public | BindingFlags.Static, null,
                                   new[] { typeof(string), retType.MakeByRefType() }, null);

    if (tParse != null)
        var parameters = new object[] { thingToParse, null };
        var success = (bool)tParse.Invoke(null, parameters);
        if (success)
            return (T)parameters[1];

    return default(T);

What If the type you are converting to doesn't implement tryParse()?

var csv = "1,2,3,4,5,6,7";
var list = someStringThatShouldBeAnInt.Parse<List<string>>();

Yea that’s not going to work. So let's give you the option to pass in your own converter.

public static T Parse<T>(this string thingToParse, Func<string, T> parser)
    return parser.Invoke(thingToParse);

Wow that’s even cleaner. Now we can do the following:

var csv = "1,2,3,4,5,6,7";
var list = csv.Parse<List<string>>(x => x.Split(',').ToList());

Yea that’s clean.

If you find this useful or see ways to improve please feel free to comment.

You can also find some other great topics over at my blog at:

This utility can be found with many others here:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Sympletech
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

GeneralMy vote of 3 Pin
Abdul Quader Mamun13-Aug-12 17:36
memberAbdul Quader Mamun13-Aug-12 17:36 
General[My vote of 2] May I suggest this ? Pin
Philippe Bouteleux6-Aug-12 1:51
memberPhilippe Bouteleux6-Aug-12 1:51 
Questionone question ... and now voted #4 Pin
BillWoodruff3-Aug-12 17:03
memberBillWoodruff3-Aug-12 17:03 
var csv = "1,2,3,4,5,6,7";
var list = someStringThatShouldBeAnInt.Parse<List<string>>();
"Yea that’s not going to work. So let's give you the option to pass in your own converter."
This still seems a bit strange, in that you do not use 'csv' in the following call to 'Parse.

thanks, Bill
"Everything we call real is made of things that cannot be regarded as real." Niels Bohr

AnswerRe: one question ... and now voted #4 Pin
Sympletech3-Aug-12 19:33
memberSympletech3-Aug-12 19:33 
GeneralNot bad Pin
PIEBALDconsult3-Aug-12 12:42
memberPIEBALDconsult3-Aug-12 12:42 
GeneralRe: Not bad Pin
Sympletech3-Aug-12 19:37
memberSympletech3-Aug-12 19:37 
GeneralRe: Not bad Pin
PIEBALDconsult7-Aug-12 11:29
memberPIEBALDconsult7-Aug-12 11:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 3 Aug 2012
Article Copyright 2012 by Sympletech
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid