Ejemplo n.º 1
0
            public void CastingInconvertibleThrowsException()
            {
                Meter meters = (Meter)5.0;

                // The following is rejected at compile time with error message: Cannot convert type 'Meter' to 'Kilogram'
                //var badSyntax = (Kilogram)meters;

                // The following is rejected at compile time too with error message: Operator '/' cannot be applied to operands of type 'Meter' and 'Kilogram'
                //var badSyntax = meters / (Kilogram)10.0;

                // But the following fails only at runtime throwing InvalidOperationException: Cannot convert type "Meter" to "Kilogram"
                var badCast = Kilogram.From(meters);
            }