C#自定义转换(implicit 或 explicit)

  C#的类型转换分为显式转换和隐式转换,显式转换需要自己声明转换类型,而隐式转换由编译器自动完成,无需我们声明,如:  

    //long需要显式转换成int
    long l = 1L;
    int i = (int)l;

    //int可以隐式的转换成long
    int i = 1;
    long l =;

  我们还可以自定义显式转换和隐式转换,分别采用 explicit 和 implicit 关键字来实现,格式:  

    //显式转换
    public static explicit operator Type_A(Type_B b)
    {
        return Instance_Of_Type_A;
    }
    //隐式转换
    public static implicit operator Type_A(Type_B b)
    {
        return Instance_Of_Type_A;
    }

  其中 Type_A 和 Type_B 是不同的类型,且必须有一个类型是当前包含这个转换的类型,如:  

    public class ClassA
    {
        public string Description { get; set; }

        public static implicit operator ClassA(ClassB classB) => new ClassA() { Description = "implicit from ClassB to ClassA:" + classB.Description };
        public static implicit operator ClassA(string description) => new ClassA() { Description = "implicit from string to ClassA:" + description };
        public static implicit operator ClassB(ClassA classA) => new ClassB() { Description = "implicit from ClassA to ClassB:" + classA.Description };
        public static implicit operator string(ClassA classA) => "implicit from ClassA to string:" + classA.Description;

        public override string ToString() => Description;
    }
    public class ClassB
    {
        public string Description { get; set; }

        public static explicit operator ClassB(string description) => new ClassB() { Description = "explicit from string to ClassB:" + description };
        public static explicit operator string(ClassB classB) => "explicit from ClassB to string:" + classB.Description;

        public override string ToString() => Description;
    }
    static void Main(string[] args)
    {
        //隐式转换
        {
            ClassA classA = "string";//字符串隐式转换成ClassA
            Console.WriteLine(classA as object);

            string description = new ClassA() { Description = "ClassA" };//ClassA隐式转换成字符串
            Console.WriteLine(description);

            ClassA classA1 = new ClassB() { Description = "ClassB" };//ClassB隐式转换成ClassA
            Console.WriteLine(classA1 as object);

            ClassB classB = new ClassA() { Description = "ClassA" };//ClassA隐式转换成ClassB
            Console.WriteLine(classB as object);

            //输出:
            //implicit from string to ClassA:string
            //implicit from ClassA to string:ClassA
            //implicit from ClassB to ClassA:ClassB
            //implicit from ClassA to ClassB:ClassA
        }

        //显式转换
        {
            ClassB classB = (ClassB)"string";//字符串显式转换成ClassB
            Console.WriteLine(classB as object);

            string description = (string)new ClassB() { Description = "ClassB" };//ClassB显式转换成字符串
            Console.WriteLine(description);

            //输出:
            //explicit from string to ClassB:string
            //explicit from ClassB to string:ClassB
        }
    }

  注:自定义的转换在开发中不常见,在一些第三方包、插件、框架中比较常见,但是有一个需要注意的点:is 和 as 运算不受自定义类型转换的影响,如:

    static void Main(string[] args)
    {
        //值类型int可以隐式转换成long,==判断为true,但是is判断为false
        int i = 1;//int
        long l = i;//int可以隐式转换为long
        Console.WriteLine(l == i);//true
        Console.WriteLine(i is long);//false

        ClassA classA = new ClassA() { Description = "ClassA" };
        ClassB classB = classA;//ClassA隐式转换成ClassB
        Console.WriteLine(classB as object);//implicit from ClassA to ClassB:ClassA
        Console.WriteLine(classA is ClassB);//false
    }

 

posted @ 2021-11-29 17:06  没有星星的夏季  阅读(439)  评论(0编辑  收藏  举报