C # Learning Diary---Display type conversion specific use cases

Source: Internet
Author: User
In the type conversion of C #, there is a type conversion-----display type conversion that requires us to declare in addition to the implicit type conversions described in the previous article.





The display type conversion, also called coercion type conversion, requires that we explicitly specify the type of conversion when making the conversion. For example, when we convert a long type to an int type, because this conversion is a conversion of lost precision, the system does not automatically do the implicit conversion, so a cast is required:


long l = 6000;
                   int i = (int) l; // needs to be used in () to declare the conversion type


Display type conversions are not valid for any of the 2 types, such as:


int i = 6000;
                   string i = (string) i; // An error will be reported here


So the display type conversions also have certain rules:


    • Display numeric conversions;

    • Display enumeration conversions;

    • Show reference conversions;


The display transformation does not always succeed, and often can cause information loss (because of the different types, range, precision is different detail reference data type), the display conversion includes all implicit conversions, so you can also write the implicit conversion in the form of a display transformation, such as:


int i = 6000;
                             long l = (long) i; // equivalent to long l = i;


Display numeric conversions:



Display numeric conversions, which are conversions between a value type and a value type, like the following rule:


    • 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, double type;


Write so many summary, that is, from high-precision to low-precision conversion, it is possible to retain the conversion may also be 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; // Show conversion The valid float is only 8 bits (. is also a bit), so round from the 9th
  
             int n_int = (int) n_double; // retain only integers
  
             Console.WriteLine ("n_float = {0} \ nn_int = {1}", n_float, n_int);
              
         }
     }
}


Operation Result:









The comparison found that when the double data range exceeds the valid range of values for float, the display converts to a 9th bit rounding, and only the integer portion is preserved when converting to type int.






Show Enumeration Transformations:



The Display enumeration transformation consists of the following content:


    • From SByte, Byte, short, ushort, int, uint, long, ulong, float, char, double, decimal type to any enumeration type;

    • From any enumerated 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 case:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
  
namespace Test
{
    class Program
    {
        enum weekday // define 2 enums
        {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
        enum Month
        {Janurary = 1, February, March, April, May, Jun, July}
        static void Main (string [] args)
        {
            int n_int = 2;
            double n_double = 3.0;
            decimal n_decimal = 5m; // declare decimal type to add m
  
            weekday weki = (weekday) n_int; // Conversion from int, double, decimal to enum
            weekday wekd = (weekday) n_double;
            weekday wekde = (weekday) n_decimal;
  
            weekday wek = weekday.Tuesday; // Conversion between enumerated types
            Month mon = (Month) wek;
  
            int i = (int) wek; // Conversion from enum type to int
            int t = (int) mon;
            Console.WriteLine ("n_int = {0} \ nn_double = {1} \ nn_decimal = {2}", weki, wekd, wekde);
            Console.WriteLine ("wek = {0} \ nmon = {1} \ nwek = {2} \ tmon = {3}", wek, mon, i, t);
              
        }
    }
}


Operation Result:






To display a reference transformation:



Conversions from an object to any reference type;


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
  
namespace Test
{
     class Program
     {
         // define 2 classes teacher and man
         class teacher
         {}
         class man
         {}
         static void Main (string [] args)
         {
             man per = new man (); // Instantiate man with an object per
             object o = per; // boxing
             teacher p = (teacher) o; // convert o display to teacher class
              
         }
     }
}


The conversion from the class type S to the 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 // Define 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; // Convert parent to child
              
         }
     }
}


The conversion from the class type S to the interface T, where S is not a sealed class and does not implement t; (the contents of the interface (interface) are written later, it declares only that methods do not define methods)



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


namespace Test
{
     class Program
     {
         public interface teacher // Define 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
                        
         }
     }
}


From the interface type S to the class type T conversion, where T is not a sealed class, and does not implement S;


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
  
namespace Test
{
     class Program
     {
         public interface man // Define an interface
         {}
         class teacher: man // Define a class man that inherits from man
         {}
         class student // Define a new class
         {}
         static void Main (string [] args)
         {
             man teac = new teacher (); // Instantiate an interface indirectly
             student stu = (student) teac; // display conversion
                          
         }
     }
}


The conversion from the interface type S to the 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 // Define an interface
         {}
         class teacher: man // derive a class from the interface
         {}
         public interface person // Define an interface
         {}
         class student: person // Derive a class from the interface
         {}
         static void Main (string [] args)
         {
             man teac = new teacher (); // Instantiate an interface indirectly
             person stu = (person) teac; // display conversion
                          
         }
     }
}


The reference type array and the reference type array display transformations, where both are the parent class's relationship to the child class (the dimensions are 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
              
         }
     }
}


Not if we change to the following array.


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; // There is an error here
              
         }
     }
}


From System.Array to array type (array is the base class for 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 of type Array and initialize it
            double [] d = new double [5];
            d = (double []) arr; // Display conversion
         }
     }
}


From System.Delegate to delegate (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 // Define a class DE that inherits from Delegate
         {}
         static void Main (string [] args)
         {
             Delegate MY = new DE (); // Indirect instantiation of the Delegate abstract class
             mydele my = (mydele) MY; // Display conversion
         }
     }
} 


The above is the C # Learning Diary---Display type conversion specific use case content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • 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.