Which of the following is better code in c# and why?
((DateTime)g[0]['MyUntypedDateField']).ToShortDateString()
or
DateTime.Parse(g[0]['MyUntypedDateField'].ToString()).ToShortDateString()
Ultimately, is it better to cast or to parse?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If g[0][‘MyUntypedDateField’] is really a DateTime object, then the cast is the better choice. If it’s not really a DateTime, then you have no choice but to use the Parse (you would get an InvalidCastException if you tried to use the cast)