Click here to Skip to main content
13,298,842 members (37,741 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 28 Oct 2012

Enhance String type to get Converted In Given Type

, 28 Oct 2012
Rate this:
Please Sign up or sign in to vote.
Enhance String type to get Converted In Given Type


Recently I was working on the project where I need to read the excel file data and have to convert data of it in strong entity which is consist of number of property. For this I know the sequence of the data in excel file and name & type of property of the entity which is going to store the value. But the real problem is I need to convert the string value in property type.


One of the solution to this is create function and pass the type & string value which
return data in type which is passed. Below is implementation function

public static T ConvertData<T>(this string s)
        if (typeof(T) == typeof(System.Decimal))
           return (T)(object)Convert.ToDecimal(s);
     //same code get replicated for int, float etc. 
     return default(T);

In above code I created on generic extension method for string type. Function convert string type to the type I want.


  • In above implementation I written code for Decimal type but same code I have to replicate for other types also i.e for int, float etc.
  • As you see in code after converting data in given type I again need to reconvert into object and than into type T again. This also add overhead of type casting.
  • Code is become long and unclear.
So to make code clean , clear and easy to understand. I fond one method in which is Convert.ChangeType which helps me to create the method i want easily.
Below is my Simpler and easy solution
public static T ConvertData<T>(this string s)
         return (T)Convert.ChangeType(s, typeof(T),null);
     return default(T);
So in above solution I just need to write one line of the code which do the task for me.
Actual syntax of method is
public static Object ChangeType(
 Object value,
 Type conversionType,
 IFormatProvider provider
As you can see in syntax third parameter is provider which is null in my case but you can pass the formater by creating as you need.
Now following code is just show how to use this method in code i.e ConvertData method
string s = "123";
int a = s.ConvertData<int>();
So this method can be used any project and also fit in number of requirement.


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


About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog

StackOverFlow -
My CV :-


You may also be interested in...


Comments and Discussions

SuggestionTypo Pin
Ambre Sachin29-Oct-12 4:33
memberAmbre Sachin29-Oct-12 4:33 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 29 Oct 2012
Article Copyright 2012 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid