(int)
is explicit cast where Convert.ToInt
is method
Consider:
string x = "1";
int temp = 0;
temp = Convert.ToInt32(x);
temp = (int)x; //This will give compiler error
Casting works for compatible types:
long l = 123;
int temp2 = (int)l;
double d = 123.2d;
int temp3 = (int)d; // holds only the int part 123
You may see Explicit cast - MSDN
if a conversion cannot be made without a risk of losing information,
the compiler requires that you perform an explicit conversion, which
is called a cast. A cast is a way of explicitly informing the compiler
that you intend to make the conversion and that you are aware that
data loss might occur.