194 Methods Chapter 6 Type Can be Converted (Unlimited web hosting)

194 Methods Chapter 6 Type Can be Converted to Type(s) bool object byte decimal, double, float, int, uint, long, ulong, object, shortor ushort sbyte decimal, double, float, int, long, objector short char decimal, double, float, int, uint, long, ulong, objector ushort decimal object double object float doubleor object int decimal, double, float, longor object uint decimal, double, float, long, ulong, or object long decimal, double, floator object ulong decimal, double, floator object short decimal, double, float, int, longor object ushort decimal, double, float, int, uint, long, ulongor object Fig. 6.5 Allowed implicit conversions. For example, the Math class method Sqrt can be called with an integer argument, even though the method is defined in class Mathto receive a double argument. The statement Console.WriteLine( Math.Sqrt( 4 ) ); correctly evaluates Math.Sqrt(4) and displays the value 2. C# implicitly converts the int value 4 to the double value 4.0 before passing the value to Math.Sqrt. In many cases, C# applies implicit conversions to argument values that do not correspond precisely to the parameter types in the method definition. In some cases, attempting these conversions leads to compiler errors because C# uses conversion rules to determine when a widening conversion can occur. In our previous Math.Sqrt example, C# converts an int to a double without changing its value. However, converting a double to an int truncates the fractional part of the double value. Converting large integer types to small integer types (e.g., long to int) also can result in changed values. Such narrowing conversions can lose data; therefore, C# does not allow narrowing conversions without an explicit cast operation. The conversion rules apply to expressions containing values of two or more data types (also referred to as mixed-type expressions) and to primitive data-type values passed as arguments to methods. C# converts the type of each value in a mixed-type expression to the highest type in the expression. C# creates a temporary copy of each value and uses it in the expression the original values remain unchanged. A method argument s type can be promoted to any higher type. Converting values to lower types can result in data loss. In cases where information could be lost through conversion, the compiler requires the programmer to use a cast to force the conversion to occur. To invoke our Square method, which takes an integer parameter (Fig. 6.3) with the double variable y, the method call would be written as
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Leave a Reply