Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# why Convert.ToInt16(num) vs ToInt16(num)?

Tags:

c#

.net

what is the difference between Convert.ToInt16(somenumber) and ToInt16(somenumber) and also vs. (ToInt16)somenumber

when do we have to use one over the other?

like image 883
Alex Gordon Avatar asked Dec 12 '22 18:12

Alex Gordon


1 Answers

Convert.Int16() is a method (probably) in System.Convert. It has a number of overloads that convert from a type to Int16

.ToInt16() could be a method defined for a particular class, but most likely you are just referring to the exact same method. Read up on namespaces.

But if you have a class called Unicorn and it has a ToInt16() method, you obviously will have to use that as there is no overload in System.Convert that supports Unicorn.

like image 129
NullUserException Avatar answered Dec 25 '22 00:12

NullUserException