Just curious:
These lines throw Invalid Cast Exception:
Unable to cast object of type 'System.Double' to type 'System.String'.
Object obj = new object();
obj = 20.09089;
string value = (string)obj;
I receive the obj value from a library.
How to simply convert to string when we don't know the type of object while enumerating?
(string)objandobj.ToString()...string value = obj.ToString();objcould benull.if (obj is double) { \\ do double stuff }Convert.ToString()does the same thing in the end. But it also checks if the given object implementsIConvertibleorIFormattablebeforehand (and calls the more appropriateToStringmethods if they do). So if you want those checks, or at least can live with them, then go for it. Yackov just showed the way with possibly the smallest footprint while still being safe.