casting - Why can't I explicitly cast some data types like string to int, byte, float, etc? C# -


i know it's impossible cast string or bool (int)mystring (short)mybool , on while it's allowed cast int short.

unfortunately not find explanation, why. i'm asking here.

why not allowed explicitly cast data types (i not asking how convert string other types or this)?

refer: casting , type conversions (c# programming guide)

explicit conversions (casts): explicit conversions require cast operator. casting required when information might lost in conversion, or when conversion might not succeed other reasons. typical examples include numeric conversion type has less precision or smaller range, , conversion of base-class instance derived class.

from: explicit conversion

explicit conversion required compilers support narrowing conversions. language-specific way perform conversion. in languages, c# , c++, explicit conversion performed using casting. casting occurs when prefix conversion data type defines type of conversion want perform. in visual basic, ctype function used allow explicit conversions of data types not allowed implicitly.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -