1. What problems do they solve?
Consider the following requirements, the person class has a field of age. What if I want to create a person object with age 18 using person P = (person) 18来?
Further, I want to use person P = 18来 to create a person object with age 18, what should I do?
2, using explicit (explicit) and implicit (implicit)
1 class Person2 {3 Private intAge ;4 Public int Age5 {6 Get{returnAge ;}7 Set{age =value;}8 }9 Ten Public Static Explicit operatorPerson (intAge ) One { A return NewPerson () {age =Age ,}; - } - the //Public static implicit operator person (int.) - //{ - //return new Person () {age = Age,}; - //} + } - + class Program A { at Static voidMain (string[] args) - { -Person P = (person) -;//Call Explicit - //person p =;//Call Implicit - } -}
Note: Both are available at the same time, otherwise compile errors. This syntax is in fact borrowed from the C + + approach, and has been extended. In general, do not use this type of conversion because it is not intuitive
C # Explicit and implicit