Actually that is expected what you are doing is an implicit conversion.
because every object has a base ToString() method when you are adding
"ABC" + b the compiler sees the left side of the assignment operator is a string and it uses what it has at its disposal to try and make all the values on the right side the same type. and since every object has at least 1 ToString() method it will always work.
it's the same thing as doing the following
int a = 10
long b = 98948989
long c = a + b
since the comiler is looking for a long to be assigned to c it implys that a needs to be converted to long which is why it's called implicit conversion
however the following will give you a comiler error
long a = 10009994
int b = 20
int c = a + b
the compiler can not implicitly convert from a larger type down to a smaller type to make this work you need an explicit conversion like the following
int c = int.Parse(a.ToString()) + b
we force the compiler to first convert the long to a string, then parse that string to turn it into an integer) now we can add them all together.
i would recomend you read up on implicit and explicit conversions in the .NET framework, they are very powerful and most often overlooked.
a few common points to remember
1. with EXPLICIT conversions you get out exactly what you tell the compiler to give
you nothing more nothing less
2. with IMPLICIT conversions you get out what the compiler thinks you need.
3. with IMPLICIT conversion the compiler will convert from smaller to larger
a. int -long
b byte -int
c. long -string
d. etc.....
4. with IMPLICIT conversion the compiler will give you an error if you try to let it try
and convert from a larger down to a smaller type
a. long -short
b. string -double
c. string -char
hope that helps clear up some confusion
From
http://www.developmentnow.com/g/36_2...conversion.htm
Posted via DevelopmentNow.com Groups
http://www.developmentnow.com