zoukankan      html  css  js  c++  java
  • C#explicit和implicit关键字实现类型转换

    using System;
    
    
    namespace ConsoleTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                //implicit 隐式装换
                Digit dig1 = new Digit(7.0m);
                decimal num = dig1;
                Digit dig2 = 12.0m;
                Console.WriteLine("num = {0} dig2 = {1}", num, dig2.Val);
    
                //explicit 显示转换
                AA a1 = new AA
                {
                    A = "A",
                    B = 1,
                    C = 1.0m
                };
    
                BB b1 = new BB
                {
                    A1 = "B",
                    B1 = 2,
                    C1 = 2.0m
                };
                AA a2 = (AA)b1;
                BB b2 = (BB)a1;
                Console.WriteLine($"{a2.A}|{a2.B}|{a2.C}");
                Console.WriteLine($"{b2.A1}|{b2.B1}|{b2.C1}");
                Console.ReadLine();
            }
        }
    
        class Digit
        {
            public decimal Val;
            public Digit(decimal d) { Val = d; }
            public static implicit operator decimal(Digit d)
            {
                return d.Val;
            }
            public static implicit operator Digit(decimal d)
            {
                return new Digit(d);
            }
        }
    
        class AA
        {
            public string A { get; set; }
            public int B { get; set; }
            public decimal C { get; set; }
    
            public static explicit operator BB(AA c)
            {
                return new BB
                {
                    A1 = c.A,
                    B1 = c.B,
                    C1 = c.C
                };
            }
        }
    
        class BB
        {
            public string A1 { get; set; }
            public int B1 { get; set; }
            public decimal C1 { get; set; }
            public static explicit operator AA(BB c)
            {
                return new AA
                {
                    A = c.A1,
                    B = c.B1,
                    C = c.C1
                };
            }
        }
    
    }
  • 相关阅读:
    Jedis测试redis
    jedis池的作用
    错误
    Ceph剖析:数据分布之CRUSH算法与一致性Hash
    drools规则引擎初探
    Techniques for HA IT Management
    django_simple_captcha使用笔记
    微服务架构的理论基础
    分布式系统服务的稳定性
    四层、七层负载均衡的区别
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/10948483.html
Copyright © 2011-2022 走看看