Y
Y
Yury Trofimov2018-10-28 19:23:31
.NET
Yury Trofimov, 2018-10-28 19:23:31

Cast vs Convert vs Parse?

Good day. I didn’t find a clear and intelligible answer on the Internet, so I’m asking here:
Please explain what is the difference and at what points it’s better to use
TryParse I’m not interested, I want to know exactly about 3 conversion methods indicated in the question
Thank you in advance)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2018-10-28
@1trofimov

Cast - if possible, it's better to use it (not always possible)
Parse - designed to work with strings, if you have a string that stores a number, then it's better to use this option. It is important to remember that if the string is null, then the method will throw an exception ArgumentNullException
Convert - for strings, it calls the Parse method inside itself, and if the string is null, then the method does not throw an exception, but returns 0. It can also accept any object, which implements the IConvertible interface, so it is better to use it when it is not known what exactly the string will come to the method. Convert also has the ability to convert to any type in generic methods. For strings, Parse is slightly faster than Convert
PS There are a lot of answers to this question on the Internet, learn to search in English - 95% of the information is there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question