In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces how to use Explicit and Implicit in C#, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.
Let's start with a strange piece of code:
If (dto.Payment = = null) continue; var entity = entries.FirstOrDefault (e = > e.LedgerEntryID = = dto.LedgerEntryID); dto.Payment = entity?.Payment
Where dto.Payment is an instance of PaymentDTO class, entity?.Payment is an instance of Payment class, PaymentDto class and PaymentDto class have no child parent relationship, so there is no implicit conversion between subclass and parent class.
Oddly enough, Visual Studio's compiler does not prompt for any compilation errors.
After opening the definition of the PaymentDTO class, you found the following method signature.
Public static implicit operator PaymentDTO (Payment payment)
In terms of method signatures, this is the operator that overrides the PaymentDTO type, but it's not the +, -, *, /, =, and so on that I used to use before.
After querying MSDN, I learned that implicit and explicit are a pair of conversion operators.
1. Implicit and Explicit1, Implicit
The Implicit keyword is used to declare implicit user-defined type conversion operators. It can realize the implicit conversion of two different classes and improve the readability of the code. However, it should be noted that after using the implicit conversion operator, exception checking is skipped at compile time, so the implicit conversion operator should never throw an exception and never lose information, otherwise there will be some unexpected problems at run time.
For example, the current definitions of 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, simply 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")};}}
When calling, you only need to assign a value directly.
Class Program {static void Main (string [] args) {PaymentDTO dto = new Payment {Amount = 1}; Console.WriteLine (dto.AmountString); Console.Read ();}} 2, Explicit
The Explicit keyword declares a user-defined type conversion operator that must be invoked by conversion. Unlike implicit conversions, explicit conversion operators must be called through conversions, and in the absence of explicit conversions, errors will occur at compile time.
For example, we now 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 point, because there is no explicit conversion in the Main method, the compiler makes an error and prompts Cannot implicitly convert type 'ExplicitImplicit.Payment' to' ExplicitImplicit.PaymentDTO'. An explicit conversion exists (are you missing a cast?)
If you want the compiler to pass the compilation, you only need to do a display conversion.
Class Program {static void Main (string [] args) {PaymentDTO dto = (PaymentDTO) new Payment {Amount = 1}; Console.WriteLine (dto.AmountString); Console.Read () }} about how to use Explicit and Implicit in C# is shared here, I hope the above content can be helpful to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.