用户自定义类型转换

来源:互联网 发布:大学c语言教材 编辑:程序博客网 时间:2024/06/05 10:06

CODE:

using System;namespace CsStudy{    class Per    {        public int p = 5;        public static implicit operator int(Per a)//自定义隐式类型转换        {            return a.p + 1;        }        public static explicit operator Per(int a)//自定义显式类型转换        {            return new Per();        }    }    class Program    {        static void Main()        {            int a = 1;            Per pr = new Per();            pr = (Per)a;            a = pr;            Console.ReadKey();        }    }}


0 0
原创粉丝点击