Implicit reference conversions for C #

Source: Internet
Author: User
Tags prototype definition

Implicit reference conversions consist of the following categories:

Conversions from any reference type to an object type

Conversion from class type S to class type T, where S is a derived class of T.

Conversion from class type S to interface type T, where class S implements interface T.

The transformation from the interface type S to the interface type T, where T is the parent interface of S.

From an array type s that has an element type of TS to an array type T that has the element type TT, this conversion requires the following conditions to be met:

S and T only have different data types for elements, but they have the same dimensions.

Both TS and TT are reference types.

There is an implicit reference conversion from TS to TT

The conversion from any array type to the System.Array.

Conversion from any representative type to System.Delegate.

Conversion from any data type or representative type to system.icloneable.

A conversion from a null type (NULL) to any reference type.

For example, the following program cannot be compiled because the element type of the array is a value type and no such implicit conversion exists in C #.

Program Listing 6-3:

Using System;
Class Test
{public
  static void Main () {
    float[] float_arr=new float[10];
    Int[] Int_arr=new int[10];
    Float_arr=int_arr
   }
}
The following procedure is correct: the

list of procedures is 6-4:

using System;
Class Class1
{} class
Class2:class1
{}
class Test
{public
   static void Main () {
     class1[] Class1_arr=new class1[10];
     Class2[] Class2_arr=new class2[10];
     Class1_arr=class2_arr
    }
}

Program 6-5 is interesting, and it gives us a prototype definition of the value types we use in the system environment.

Program 6-5:

Using System;
Class Test
{public
  static void Main () {
    float[] float_arr=new float[10];
    Double[] Double_arr=new double[10];
    Sbyte[] Sbyte_arr=new sbyte[10];
    Byte[] Byte_arr=new byte[10];
    Ushort[] Ushort_arr=new ushort[10];
    Int[] Int_arr=new int[10];
    Long[] Long_arr=new long[10];
    String[] String_arr=new string[10];
    Console. WriteLine (Float_arr);
    Console.WriteLine (Double_arr);
    Console.WriteLine (Sbyte_arr);
    Console.WriteLine (Byte_arr);
    Console.WriteLine (Ushort_arr);
    Console.WriteLine (Int_arr);
    Console.WriteLine (Long_arr);
    Console.WriteLine (String_arr);
   }

The output result of the program is:

system.single[];
System.double[];
System.sbyte[];
System.byte[];
System.int16[];
System. Int32[];
System.int64[];
System.string[];

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.