Can you please explain the difference between explicit
and implicit
type casts?
This is a little tricky because the "cast" syntax in C# actually does a range of different things (cast, primitive convert, bespoke convert, etc)
In an implicit cast, there is an obvious reference-preserving conversion between the two:
List<int> l = new List<int>(); IList<int> il = l;
The compiler can prove that this is safe just from static analysis (List<int>
is always an IList<int>
)
With an explicit cast, either you are telling the compiler that you know more than it does - "please believe me, but check anyway":
List<int> l = new List<int>(); IList<int> il = l; List<int> l2 = (List<int>)il;
Although this cast is possible, the compiler won't accept that all IList<int>
s are actually List<int>
- so we must tell it to let it by.
In an implicit primitive conversion (providedby the language spec), it is generally assumed that there is a safe, non-risky, non-lossy (caveat: see Jon's comment) conversion:
int i = 1; float f = i;
With an explicit primitive conversion, it is likely that the conversion could lose data, or is non-obvious:
float f = 1; int i = (int)f;
With bespoke operators, all bets are off, and you'd have to look at the documentation. It could be a reference-cast, or it could be anything. It may follow similar rules to primitive conversions (example: decimal
), or it could do anything randomly:
XNamespace ns = "http://abc/def"; // implicit XAttribute attrib = GetAttrib(); int i = (int)attrib; // explicit (extracts text from attrib value and // parses to an int)
Both of these run custom code that is context-specific.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With