John Virgolino John Virgolino - 1 year ago 125
C# Question

C# Casting vs. Parse

This may seem rudimentary to some, but this question has been nagging at me and as I write some code, I figured I would ask.

Which of the following is better code in c# and why?




Ultimately, is it better to cast or to parse? Thanks all!

Answer Source

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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download