C # Learning Diary-----implicit conversion of specific use cases

Source: Internet
Author: User
After learning about the basic data types in C # in the previous section, we will learn about the conversions between the types below. Type conversions in C # can be divided into 2 classes: implicit conversions and explicit conversions.

Implicit conversions:

Implicit conversions are system-default conversions that can be converted without the need for a declaration. In the implicit conversion process, the compiler does not need to examine the transformation to perform the conversion safely, such as from the int type to the long type, or to the implicit conversion. Implicit conversions generally do not fail, and no information is lost during the conversion process.

For example: int i = 100;

Long a = i; No need to declare automatic conversion of int to type long

Implicit conversions are not valid for any two types, for example, if we implicitly convert the long type above to an int type it will not succeed:

Long a = 100;

int i = A; Compiler will error

Therefore, implicit conversions have the following rules:

    • Implicit numeric conversions

    • Implicit enumeration conversions

    • Implicit reference conversions

    • Implicit numeric conversions:

Implicit numeric conversions include the following:

    • From sbyte type to short, int, long, float, double, decimal type;

    • From byte type to short, ushort, int, uint, long, ulong, float, double, decimal type;

    • From the short type to int, long, flaot, double, decimal type;

    • from ushort type to int, uint, long, ulong, Flaot, double, decimal type;

    • from type int to long, flaot, double, decimal type;

    • From uint type to long, ulong, Flaot, double, decimal type;

    • From long type to float, double, decimal type;

    • From ulong type to float, double, decimal type;

    • From char type to ushort, int, uint, long, ulong, Flaot, double, decimal type;

    • From float type to double type;

Write so much to summarize, in summary, from the low-precision type to the high-precision type conversion (because there is no loss of precision and data information), and from high-precision type to low-precision can not be implicitly converted (may lose some information, unsafe). Refer to C # learning Diary 04 for the type of accuracy and scope. The reminder here is that there is no implicit conversion of other types to char types.

An example of an implicit numeric conversion:

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {        class program      {          static void Main (string[] args)          {              byte x = 255;      BYTE represents the range 0~255 short              y = x;      The implicit conversion of y++ from byte to short              ;              Console.WriteLine ("y = {0}", y);                y = 32767; Shot range -32768~32767                int i = y + 5;  The implicit conversion from short to int extends the range result is accurate              y+=5;          Out of range The result will be inaccurate              Console.WriteLine ("y = {0}", y);//y out of range data will lose part                            Console.WriteLine ("I = {0}", i);}      }  }

Results:

From this example, it can be seen that the timely adoption of type conversion is very important.

Implicit enumeration conversions:

The implicit enumeration conversion allows the conversion of decimal 0 to any enumeration type, noting that it can only convert 0, and that there is no such implicit conversion to other integers, see the following example:

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {        class program      {          enum weekday  //Define an enumeration type          {Sunday, Monday, Tuesday, Wednesday , Thursday, Friday, Saturday};          static void Main (string[] args)          {              weekday day;              Day = 0;      Implicitly converts 0 to an enumeration type (only 0)              Console.WriteLine (day);}}                        }  

The output is:

  Sunday

In the above code, if we put day = 0 as Day = 1 The compiler will give an error.

Implicit reference conversions:

    • Conversions from any reference type to an object type;

      (Person p = new person ())

    • The conversion from the class type S to the class type T, where S is the derived class of t;

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {      class person     //defines a base class (parent) person      {       }      class Person1:person   // Person1 derived from the base class Person,person1 is called a subclass of person,      {       } class program      {          static void Main (string[] args)          {              Person1 per = new Person1 ();  Instantiates a subclass of Person1 an object per person              per = per;        Implicitly convert subclass to parent class                                      }}  }

A conversion from the class type S to the interface type T, where the class S implements the interface T; (the contents of the interface (interface) are written later, using it to declare only methods that do not define methods)

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {public      interface Infa   //Defines an interface      {          void Output ();      }      Class PERSON:INFA    //defines a person class that inherits from an interface and implements the method      {public          void Output ()          {              Console.WriteLine (" Welcome ");          }      }      Class program      {          static void Main (string[] args)          {person              per = new Person ();  Instantiation of                Infa fa = per;    Implicit conversion from person to Interface (interface)}}}  

The conversion from the interface type S to the interface type T, where T is the parent interface of S;

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {public      interface Infa   //Defines an interface      {          void Output ();  The interface only declares the method, and the implementation is determined by its derived class write code      } public      interface Infa1:infa    //defines a INFA1 interface that inherits from the Infa interface      {          void Input ();      }      Class PERSON1:INFA1  //derives a Person1 class from INFA1 because the interface cannot instantiate      {       } class program      {          static void Main (string[] args)          {              Person1 per = new Person1 {};  Interfaces cannot be instantiated directly and need to instantiate a INFA1 interface derived from the Person1 class                Infa fa = per;    Implement subinterface to parent pretext implicit conversion}}                        }  

The reference-type array s to the reference-type array T-transform, where S is the derived class of T, and the array has the same number of dimensions;

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {class person      //defines a base class the person       {} class      Person1:person  //Radical class derives a subclass Person1      {       }      class program      {          static void Main (string[] args)          {              person1[] per = new person1[ 5];  Instantiate a Person1               person[] per = per;    Implementing an implicit Conversion                        }}}  

The warning here is that if the array of reference types is an array of value types, the following code will be an error:

Class program      {          static void Main (string[] args)          {              int[] n_int = new INT[10];              double[] N_doubel = new DOUBLE[10];              N_doubel = N_int;  Here's an error.                        }      }

Conversion from array type to System.Array (array is the base class for all arrays refer to previous ^_^)

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {      class program      {          static void Main (string[] args)          {              int[] n_int = new int[10];< c10/>//instantiates an array of type int n_int              array arr = n_int;  Array is a representation of arrays so it cannot be array[] arr          }      }

Conversions from any delegate type to System.Delegate; (delegate will be written later)

Using System;  Using System.Collections.Generic;  Using System.Linq;  Using System.Text;    Namespace Test  {      class program      {public          static int output (int s)  //define a method          {              Console.WriteLine ("Welcome,{0}", s);              return 1;          }            public delegate int Mydel (int s);  Declare a delegate (I'll talk about the delegate later)                    static void Main (string[] args)          {              Mydel my = new Mydel (output);   Delegate the output method to my              Delegate Mydel = my;    Implicit conversion to Mydel}}}  

The above is the C # learning Diary----Implicit conversion of the content of specific use cases, 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.