Home > Backend Development > C#.Net Tutorial > C# custom implicit and explicit conversion

C# custom implicit and explicit conversion

黄舟
Release: 2017-02-16 11:27:23
Original
1750 people have browsed it

Explicit and implicit are conversion operators. Using these two can allow our custom types to support mutual exchange.
explicti means explicit conversion, such as from A -> B, forced type conversion is required. (B = (B)A)
implicit means implicit conversion, such as from B -> A just need to assign directly (A = B)

##Implicit Conversion can make our code look more beautiful, concise and easy to understand, so it is best to use implicit operators more. but! If the object itself will lose some information (such as precision) during conversion, then we can only use the explicit operator so that the client can be warned at compile time

namespace OperatorTest
{
    /// <summary>
    /// 猪
    /// </summary>
    public class Pig
    {
        public Pig(string name)
        {
            this.Name = name;
        }
        public string Name;

        //explicit 关键字用于声明必须使用强制转换来调用的用户定义的类型转换运算符。
        //static explicit operator target_type { source_type identifier }
        //target_type 目标类型
        //source_type 源类型
        //identifier Something
        /*转换运算符将源类型转换为目标类型。源类型提供转换运算符。与隐式转换不同,必须通过强制转换的方式来调用显式转换运算符。如果转换操作可能导致异常或丢失信息,则应将其标记为 explicit。这可以防止编译器无提示地调用可能产生无法预见后果的转换操作*/
        public static implicit operator Pig(Monkey value)
        {
            Pig mk = new Pig(value.Name + ":猴子变猪!!");
            return mk;
        }
    }
Copy after login
namespace OperatorTest
{
    /// <summary>
    /// 猴子
    /// </summary>
    public class Monkey
    {
        public Monkey(string name)
        {
            this.Name = name;
        }
        public string Name;

        //implicit 关键字用于声明隐式的用户定义类型转换运算符。
        //static implicit operator target_type { source_type identifier }
        public static explicit operator Monkey(Pig value)
        {
            Monkey mk = new Monkey(value.Name + ":猪变猴子!!");
            return mk;
        }
    }
Copy after login

Invocation:


 Monkey monkey = new Monkey("悟空");
            //隐式转换 猴子变猪
            Pig monkeyToPig = monkey;
            MessageBox.Show(monkeyToPig.Name);

            Pig pig = new Pig("八戒");
            //显式转换 猪变猴子
            Monkey pigToMonkey = (Monkey)pig;
            MessageBox.Show(pigToMonkey.Name);
Copy after login

Output:



Application:
For example, in actual operation, if an object A (or an entity) is serialized into xml for storage, then the object can be An explicit operator is defined in class A to convert XML content that meets the requirements into objects or entities.
Of course, you can also define a function in the class of object A to implement this processing, but this may require one more step, that is, when the function is not a static function, you need to instantiate the object first to call the corresponding processing. function.


##Reference:

explicit keyword


implicit keyword


operator keyword



demo download

The above is the content of C# custom implicit and explicit conversion. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template