Click here to Skip to main content
15,608,989 members
Articles / Programming Languages / C#
Posted 28 Oct 2021

Tagged as


3 bookmarked

A ToString Replacement that Recognizes Different Data Types

Rate me:
Please Sign up or sign in to vote.
2.00/5 (4 votes)
28 Oct 2021CPOL1 min read
Most data types have a built-in ToString method, but not all, and what's more, the default method can throw an error if, for example, the object is null.
Not all data types have a built-in ToString method. Moreover, built-in methods can throw an error if, for example, an object is empty, or can only return the type name. This tip shows how to build a universal method (for example in extensions) that recognizes the type of data and converts it into a meaningful text string.

Using the Code

The method can be placed directly in the program class or extension class. Remember to remove the static and this declarations if you are using a dynamic class.

public static string AsString(this object oValue, bool ClearlyNamed = false)
    if (oValue == null) return ClearlyNamed ? "<null>":"";

    if (oValue.GetType() == typeof(string)) return oValue as string;

    if (oValue.GetType() == typeof(string[])) return string.Join(";",(string[])oValue));

//Recognition and processing of other types of data

    return ClearlyNamed?("<"+oValue.getType().Name+">"):"";

In the first line, the method detects if the object is empty and returns an empty string or description.
Then, in subsequent if blocks, it recognizes what type of data we are trying to present as a string and converts the data into text. The last line returns a text value if the data is not null but we cannot (or do not want to) determine its type.

The optional parameter ClearlyName forces the method not to process an null value or an unknown type into an empty string, but a description of what the input is.

It depends on the imagination of the coder whether it introduces the recognition of the next possible types of input data and the ways of processing it into text.

Comments and Explanations

Colleague Phil.o noticed well that the originally used method of merging the text from the array (using string1 + = string2) is not optimal and StringBuilder should be used. I corrected the source code as noted, but used a simpler method using string.Join.


  • 28th October, 2021: Initial version


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

Written By
Poland Poland
Retired administrator of Microsoft systems, such as corporate AD and corporate Echange e-mail, creator of their on-prem implementation, assembling programs for his own use since 1982 (then in machine code 8080, today most often C #)

Comments and Discussions

QuestionAll Types DO have a built in ToString() Method. Pin
#realJSOP31-Oct-21 23:15
mva#realJSOP31-Oct-21 23:15 
QuestionJSON serialization Pin
Christo Boshoff29-Oct-21 1:02
Christo Boshoff29-Oct-21 1:02 
AnswerRe: JSON serialization Pin
Tomasz Malicki29-Oct-21 1:45
Tomasz Malicki29-Oct-21 1:45 
SuggestionCan be improved Pin
Paulo Zemek28-Oct-21 9:20
Paulo Zemek28-Oct-21 9:20 
The presented solution might work for small projects, but it falls short if we have objects crossing library boundaries, or if we want to just respect the open/closed principle.

A simple, yet very powerful solution, would be to register Func<object, string> delegates using a Dictionary<Type, Func<object, string>>.
This way, at application start (or where conveniently) you can register all the "ToString" conversions you might need, without having to keep modifying this class.

So, this class could live in a library (let's say library A), have 0 dependencies on library B... yet, the app using both libraries could make a registration on how to convert objects from library B using library's A class responsible for doing ToString conversions.

GeneralRe: Can be improved Pin
Tomasz Malicki28-Oct-21 23:46
Tomasz Malicki28-Oct-21 23:46 
GeneralThoughts Pin
PIEBALDconsult28-Oct-21 4:07
professionalPIEBALDconsult28-Oct-21 4:07 
GeneralRe: Thoughts Pin
Tomasz Malicki28-Oct-21 23:48
Tomasz Malicki28-Oct-21 23:48 

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.