What Is 'Narrowing' And How To Use It?
Programming languages like VB, allows by default narrowing conversions. Type conversions, boxing, unboxing, narrowing and widening are all related concepts here. (see elsewhere).
Narrowing refers to where these transformations/modifications type actions taken/performed, in actual fact end up altering a value from one type to another, BUT then the destination type it turns into, cannot accommodate some or all possible values from the source type. In a sense it ‘shrinks’ or narrows, constricts or tapers.
It is explicit in C# and visual basic if option strict is on.
It may in fact return an incorrect result or error, if the source value exceeds the destination value, perfectly logically understood. If there is a lack of definition and specification here narrowing will occur and a compile error thrown out.
Some of the methods might include: Type-cast, Type-conversions, IConvertible interface enabled, (ToString, Parse), from string to base type, between baseVB types, between types using DirectCast. Many of the actions and decisions taken here for type-conversion depends on the type of conversion you want to end up with or do. Simplification, overrides and enablers all possible. Use of the narrowing/explicit keyword for those conversions where precision-loss is possible, mastery of implementation of conversion operators for custom types form part of the process.
Key Exam Points
-
Review the chapter on .NET fundamentals, types, type conversions.
-
For the exam, study all provided step-by-step examples for tyep conversion, narrowing/widening, that help you to gain a better understanding of this and related topics
-
Take the practice test, with all the specified exercises, lab activity and hands-on custom illustrative scenarios
Related Terms
Boxing, Unboxing, Casting, Constraint, Widening
This article is based on the 2nd edition of the Microsoft .Net Framework Application Training Kit with the purpose to help 70-536 Exam takers to succeed. I constantly look for ways to improve the content. Please leave a comment about this article or drop me a message if you would like to see changes for this site.





