C # Learning diary 17 --- display type conversion example

Source: Internet
Author: User

C # Learning diary 17 --- display type conversion example

In C # type conversion, except forImplicit type conversionThere is also a type conversion that needs to be declared ----- display type conversion.

Display type conversion, also known as forced type conversion, must be explicitly specified during conversion. for example, when we convert the long type to the int type, the system will not automatically perform implicit conversion because of the loss of precision. Therefore, we need to perform forced conversion:

Long l = 6000;

Int I = (int) l; // declare the Conversion Type in ()

Display type conversion is not true for any two types, for example:

Int I = 6000;

String I = (string) I; // an error is reported here.

Therefore, the display type conversion rules also apply:

Display value conversion; display enumeration conversion; display reference conversion;

The display conversion is not always successful, and often leads to information loss (because of different types, the range and accuracy are also different)Reference data type for details), Display conversions include all implicit conversions, so you can also write implicit conversions as display conversions, such:

Int I = 6000;

Long l = (long) I; // It is equivalent to long l = I;

Display numeric conversion:

Display numeric conversion refers to the conversion between the value type and the value type. The following rules apply:

From sbyte to byte, ushort, uint, ulong, char type; from byte to sbyte, char type; from short to sbyte, byte, ushort, uint, ulong, char type; from ushort to sbyte, byte, short, char type; from int to sbyte, byte, short, ushort, uint, ulong, char type; from uint to sbyte, byte, short, ushort, int, char type; from long to sbyte, byte, short, ushort, int, uint, ulong, char type; from ulong to sbyte, byte, short, ushort, int, uint, long, char type; from char to sbyte, byte, short type; from float to sbyte, byte, short, ushort, int, uint, long, ulong, char, decimal type; from double to sbyte, byte, short, ushort, int, uint, long, ulong, float, char, decimal type; from decimal to sbyte, byte, short, ushort, int, uint, long, ulong, float, char, and double;

After writing so many summaries, the conversion from high precision to low precision may be either a reserved conversion or a rounding conversion. Write an example:

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {static void Main (string [] args) {double n_double = 1.73456789; float n_float = (float) n_double; // display that the float conversion is valid for only 8 bits (. it is also a bit), so the rounded int n_int = (int) n_double; // only the integer Console is retained. writeLine (n_float = {0} n_int = {1}, n_float, n_int );}}}


Running result:

 

The comparison shows that when the double data range exceeds the float valid value range, the value is rounded to 9th BITs during conversion, and only the integer part is reserved when the value is converted to the int type.

 

Display enumeration conversion:

The display enumeration conversion includes the following:

From sbyte, byte, short, ushort, int, uint, long, ulong, float, char, double, decimal type to any enumeration type; from any enumeration type to sbyte, byte, short, ushort, int, uint, long, ulong, float, char, double, decimal type; from any enumeration type to any other Enumeration type;

Write a column:

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {enum weekday // defines two enumerations: {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday} enum Month {Janurary = 1, February, march, CMDL, May, Jun, July} static void Main (string [] args) {int n_int = 2; double n_double = 3.0; decimal n_decimal = 5 m; // declare that m weekday weki = (weekday) n_int is required for the decimal type; // convert from int, double, decimal to enumeration to weekday wekd = (weekday) n_double; weekday wekde = (weekday) n_decimal; weekday wek = weekday. tuesday; // conversion between enumeration types Month mon = (Month) wek; int I = (int) wek; // conversion from Enumeration type to int t = (int) mon; Console. writeLine (n_int = {0} n_double = {1} n_decimal = {2}, weki, wekd, wekde); Console. writeLine (wek = {0} mon = {1} wek = {2} mon = {3}, wek, mon, I, t );}}}


Running result:

 

Show reference conversion: conversion from an object to any reference type;
Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {// defines two classes: teacher and man class teacher {} class man {} static void Main (string [] args) {man per = new man (); // instantiate man into an object per object o = per; // boxed teacher p = (teacher) o; // convert o display to teacher class }}}

 

Conversion from class type s to class type t, where s is the base class of t;
Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {class man // defines a base class {} class student: man // student inherits man {} static void Main (string [] args) {man per = new man (); // man instantiates an object per student stu = (student) per; // converts the parent class to a subclass }}}

 

The conversion from class type s to interface t, where s is not a sealed class and does not implement t; (the content of the interface will be written later, it only declares methods and does not define methods)

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace Test
{
Class Program
{
Public interface teacher // defines an interface
{}
Class student // define a class
{}
Static void Main (string [] args)
{
Student stu = new student (); // instantiate an object
Teacher tea = (teacher) stu; // display Conversion

}
}
}

The conversion from the interface type s to the class type t, where t is not a sealing class, and s is not implemented;
Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {public interface man // defines an interface {} class teacher: man // defines a class that inherits from man. man {} class student // defines a new class {} static void Main (string [] args) {man teac = new teacher (); // indirectly instantiate an interface student stu = (student) teac; // display conversion }}}

 

Conversion from interface type s to interface type t, where s is not a sub-interface of t;
Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {public interface man // defines an interface {} class teacher: man // a class {} public interface person is derived from an interface // defines an interface {} class student: person // a class {} static void Main (string [] args) {man teac = new teacher (); // indirectly instantiate an interface person stu = (person) teac; // display conversion }}}
Reference TypeArray and Reference TypeArray Display conversion, where the two are the relationship between the parent class and the Child class (the dimension must be the same)
Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {class teacher {} class student: teacher // studnet inherits teacher {} static void Main (string [] args) {teacher [] teac = new teacher [5]; student [] stu = new student [5]; stu = (student []) teac; // display conversion }}}


If you replace it with the following array, it won't work.

Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {static void Main (string [] args) {double [] n_double = new double [5]; float [] n_float = new float [5]; n_float = (float []) n_double; // an error occurred }}}

 

From System. Array to array type (Array is the base class of all array types)
Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {static void Main (string [] args) {Array arr = new Array [5]; // define an Array and initialize double [] d = new double [5]; d = (double []) arr; // display conversion }}}

 

From System. Delegate to Delegate type
Using System; using System. collections. generic; using System. linq; using System. text; namespace Test {class Program {public static delegate int mydele (); // declare a delegate class DE: delegate // defines a Delegate class DE {} static void Main (string [] args) {Delegate MY = new DE (); // indirectly instantiate the Delegate abstract class mydele my = (mydele) MY; // display conversion }}}

 

 

The conversion is introduced here. Your comments and suggestions are my motivation to improve myself. Some content in this article has not been described before and will be written later. Thank you for reading ^_^ ......

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.