purpose of life is joy

NAVIGATION - SEARCH

How to : get content data type inside a string in C#?

By default, .NET have the features of identifying the data type inside a string. Everybody using this feature already in our day-to-day development activity. i.e. none other than TryParse. This TryParse method will help us in identifying the right type from the string.

With the TryParse keyword of each data type I have created a method to return the type of the passing parameter. You can add other type or custom object types to this method which I explained it here.

 

private string GetType(string types)
        {
            string returnType = "object";
            decimal de;
            int i;
            double d;
            long l;
            bool b;

            if (int.TryParse(types, out i))
            {
                returnType = "int";
            }
            else if (long.TryParse(types, out l))
            {
                returnType = "long";
            }
            else if (double.TryParse(types, out d))
            {
                returnType = "double";
            }
            else if (decimal.TryParse(types, out de))
            {
                returnType = "decimal";
            }
            if (bool.TryParse(types, out b))
            {
                returnType = "bool";
            }
            return returnType;
        }
blog comments powered by Disqus
Protected by Copyscape Web Plagiarism Check
DMCA.com