Cast type exception

Why statement

(int)"84" 

throws an exception and

 Convert.ToInt32("84") 

doesn't throw an exception?

+4
source share
7 answers

The first is simple casting, which changes the type of an object (more technically speaking, this is not casting, but a type conversion) .. NET does allow some conversions (for example, from int to long , etc.), but this particular one is forbidden. The reason I think this is forbidden is because only a small subset of strings can actually be converted to int , and the rules for this will be very cumbersome. In addition, this may adversely affect internationalization.

The second way is to call a method that actually parses the string representation of an integer and builds an int from it.

+8
source

This is because Convert.ToInt32 is a method call that takes a string.

(int) is an explicit conversion from one type to another. String and Int32 are not explicitly and implicitly convertible.

Addition on conversion: You can create your own implicit and explicit conversion, for example:

 class Foo { bool b = (bool)new Blah(); struct Blah { public static explicit operator bool(Blah b) { return true; } } } 

I kept my example very simple (and useless), but it should clearly indicate that only if the operator exists in the class or structure to be converted, a simple conversion is possible. Otherwise, you will need to use the Convert class or the IConvertible interface.

+3
source

In the .NET platform, only certain primitive type conversions are allowed. (int) "string" is not one of them. You must use the Convert class or wrapper classes

 Integer.Parse("string"); 
+1
source
 (int)"84" 

What you are trying to do is what is called explicit casting. Explicit casting only works for types or references to objects that are defined as compatible. See This for a valid conversion http://msdn.microsoft.com/en-us/library/yht2cx7b.aspx

 Convert.ToInt32("84") 

This operator itself calls the method that parses, just like I or you will write, if it is not provided by the framework. It is you who use the reflector and look at the implementation, the above method is actually implemented below, so it is much more than just telling the compiler that you want to treat the type as another type using an explicit cast.

 public static int ToInt32(string value) { if (value == null) { return 0; } return int.Parse(value, CultureInfo.CurrentCulture); } 
+1
source

Convert is designed to convert one type to another and does not rely on the ability to translate.

When casting, you can use only related types, and int and String are not connected.

Integer.Parse may interest you.

Martin.

0
source

because there is no explicit conversion from string to int. The Convert class does the conversion in the method, not just using

0
source

Casting is like pretending that one thing is another. You can successfully pretend that 27L == 27m or 27L == (int)27 , but pretending to be "84" is int , if possible, would result in 3670068 , not 84 .

Why?

Because "84" stored as two unicode characters '\x0038' and '\x0034' , not the value 84 . Convert.ToInt32("84") converts "84" to (int)84 .

Casting 4294967295U (note that this is unsigned) to an int result of -1

 uint x = 4294967295U; //need to assign this to a variable as compile will error Console.WriteLine((int)x); //results in -1 
0
source

Source: https://habr.com/ru/post/1305526/


All Articles