Why does the statement
(int)"84"
throw an exception开发者_StackOverflow and
Convert.ToInt32("84")
does not throw an exception?
First one is plain casting, which is changing a type of an object (more technically speaking, this is not casting, but type conversion). .NET indeed allows some conversions (like int
to long
, etc), but this particular one is disallowed. The reason I think this is disallowed is because only a small subset of strings can be actually converted to int
and rules for doing so will be very cumbersome. Additionally, this might not play well with internationalization.
The second is a method invocation, which actually parses string representation of an integer and constructs an int
out of it.
Thats because Convert.ToInt32
is a method call accepting a string.
(int)
is an explicit conversion from one type to another. String and Int32 are not explicitely nor implicitely convertible.
Addition about conversion: You can create your own implicit and explicit conversion, like:
class Foo {
bool b = (bool)new Blah();
struct Blah {
public static explicit operator bool(Blah b) {
return true;
}
}
}
I've kept my example very simple (and useless), but it should make the point clear that only when an operator exists in the class or struct to be converted, that a simple conversion is possible. Otherwise you'll need to use the Convert class or the IConvertible interface.
There are only certain primitive type conversions allowed in the .NET framework. (int)"string" is not one of them. You need to use the Convert class, or wrapper classes
Integer.Parse("string");
(int)"84"
What you are attempting there is what called explicit casting. Explicit casting only work with for types or object references that are defined as compatible. See this for the conversion allowed http://msdn.microsoft.com/en-us/library/yht2cx7b.aspx
Convert.ToInt32("84")
This statement itself is invoking a method that perform parsing just like what me or you would write if it is not provided by the framework. It you use reflector and look at the implementation, the above method is actually implemented as below so it is much more than just telling the compiler that you want to treat the type as another type through explicit casting.
public static int ToInt32(string value)
{
if (value == null)
{
return 0;
}
return int.Parse(value, CultureInfo.CurrentCulture);
}
Convert is designed for the purpose of converting one type to another, and does not rely on being able to cast.
In casting, you can only cast between related types, and int and String are not related.
Integer.Parse might be of interest to you as well.
Martin.
it's because there is no explicit conversion from string to int. The Convert class does the conversion in a method, not just by a cast
Casting is like pretending that one thing is another.
You can successfully pretend that 27L == 27m
, or 27L == (int)27
, but pretending that "84"
is an int
would, if it were possible, result in 3670068
, not 84
.
Why?
Because "84"
is stored as the two unicode characters '\x0038'
and '\x0034'
, rather than the value 84
.
Convert.ToInt32("84")
translates "84"
to (int)84
.
Casting 4294967295U
(note that this is unsigned) to an int
results in -1
uint x = 4294967295U; //need to assign this to a variable as compile will error
Console.WriteLine((int)x); //results in -1
精彩评论