[Unity] Unity C# Basics (15) implicit implicit type conversion, explicit explicit type conversion


implicit implicit type conversion

The implicit keyword is used to declare an implicit user-defined type conversion operator. You can use this keyword to perform implicit conversions between user-defined types and other types if you can ensure that the conversion process will not cause data loss.

After using the implicit conversion operator, exception checking will be skipped at compile time, so the implicit conversion operator should never throw an exception and never lose information, otherwise some unexpected problems will occur at runtime.

For example, the definitions of the current PaymentDTO and Payment are as follows

public class Payment
{
    
    
     public decimal Amount {
    
     get; set; }
}


public class PaymentDTO
{
    
    
     public string AmountString {
    
     get; set; }
}

If you need to implicitly convert Payment to PaymentDTO, you only need to declare the implicit conversion operator of PaymentDTO

public class PaymentDTO
{
    
    
    public string AmountString {
    
     get; set; }

    public static implicit operator PaymentDTO(Payment payment)
    {
    
    
        return new PaymentDTO
        {
    
    
            AmountString = payment.Amount.ToString("C2")
        };
    }
}

You only need to assign a value directly when calling

class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        PaymentDTO dto = new Payment {
    
     Amount = 1 };

        Console.WriteLine(dto.AmountString);
        Console.Read();
    }
}

Explicit explicit type conversion

The Explicit keyword declares a user-defined type conversion operator that must be invoked by conversion. Unlike implicit conversions, explicit conversion operators must be invoked through conversions, and if there is no explicit conversion, an error will be generated at compile time.

For example, now we change the conversion operator defined in the previous PaymentDTO class from Implicit to Explicit

public class PaymentDTO
{
    
    
     public string AmountString {
    
     get; set; }

     public static explicit operator PaymentDTO(Payment payment)
     {
    
    
         return new PaymentDTO
         {
    
    
             AmountString = payment.Amount.ToString("C2")
         };
     }
 }

At this time, because there is no explicit conversion in the Main method, the compiler makes an error, prompting Cannot implicitly convert type 'ExplicitImplicit.Payment' to 'ExplicitImplicit.PaymentDTO'. An explicit conversion exists (are you missing a cast? If you want the compiler to
insert image description here
pass To compile, just do a display conversion

class Program
{
    
    
    static void Main(string[] args)
    {
    
    
        PaymentDTO dto = (PaymentDTO)new Payment {
    
     Amount = 1 };

        Console.WriteLine(dto.AmountString);
        Console.Read();
    }
}

Summarize

  • Implicit improves the readability of the code, but the programmer needs to ensure that the conversion does not throw an exception and does not lose information.
  • Explicit prevents the compiler from silently invoking conversion operations that could have unintended consequences.
  • The former is easier to use, the latter clearly indicates to everyone reading the code that you are converting the type.

This article is reproduced from: Explicit and Implicit in C# , thanks for sharing.

For more information, please check the general catalog [Unity] Unity study notes catalog arrangement

Guess you like

Origin blog.csdn.net/xiaoyaoACi/article/details/130055322