C # Custom implicit and explicit transformations

Source: Internet
Author: User
Explicit and implicit belong to the conversion operators, which allow us to customize the type to support mutual exchange
Explicti represents an explicit conversion, such as a forced type conversion from A to B (b = (b) a)
Implicit represents an implicit conversion, such as a direct assignment from B to a (a = B)

Implicit conversions can make our code look prettier and more concise, so it's best to use the implicit operator more. But! If the object itself loses some information (such as precision) during the conversion, then we can only use the explicit operator so that the client can be warned at compile time to call

namespace operatortest{//<summary>//pig///</summary> public class pig {public Pig (string name) {this.        name = name;        } public string Name;        The explicit keyword is used to declare user-defined type conversion operators that must be invoked using casts.        Static explicit operator Target_type {source_type identifier}//target_type destination type//source_type Source Type The identifier Something/* Conversion operator converts the source type to the target type. The source type provides the conversion operator. Unlike implicit conversions, an explicit conversion operator must be invoked by casting. If the conversion operation can result in an exception or loss of information, it should be marked as explicit. This prevents the compiler from silently invoking conversion operations that may have unforeseen consequences */public static implicit operator pig (Monkey value) {Pig mk = new Pig (value. Name + ": Monkeys become pigs!!            ");        Return MK; }    }
namespace operatortest{//<summary>//monkeys///    </summary> public    class Monkey    { Public        Monkey (string name)        {This            . name = name;        }        public string Name;        The implicit keyword is used to declare an implicit user-defined type conversion operator.        //static implicit operator Target_type {source_type identifier} public        static explicit operator Monkey (Pig VA Lue)        {            Monkey mk = new Monkey (value. Name + ": Pig change monkey!! ");            return MK;        }    }

Call:


Monkey Monkey = new Monkey ("Goku");            The implicit conversion of monkeys to pigs pig            monkeytopig = monkey;            MessageBox.Show (monkeytopig.name);            Pig pig = new Pig ("Eight commandments");            Explicit conversion of pig to monkey            Monkey Pigtomonkey = (Monkey) pig;            MessageBox.Show (Pigtomonkey.name);


Output:




Application:
For example, in an actual operation to serialize an object a (or an entity) into XML for storage, you can define a explicit operator in the class of that object A to convert the conforming XML content into an object or entity.
Of course, you can also define a function in object A's class to implement this processing, but it is possible to take one more step, that is, when the function is not a static function, you need to instantiate the object to invoke the corresponding handler function.


Reference:

Explicit keywords

Implicit keywords

operator keywords


Demo download

The above is the content of C # custom implicit and explicit conversion, 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.