I have a char type column in table. I want to convert it to a char in C#.
It is working by
char gender = Convert.ToChar(dataRow["Gender"]);
But it failed
char gender = (char)(dataRow["Gender"]);
If a column is int type. I can use (int) to cast an object.
Why it failed on char(exception)?
InValidCastException.
Thanks,
The
datarow[column]is an expression of type ‘object’The
Convert.ToChar()method has an overload that takes an object as argument, so the compiler knows what to do.The
(char)dataRow[column]is an explicit cast ofdataRow[column]to the char type, but an automatic conversion from object to char doesn’t exist