阅读背景:

用户自定义类型转换

来源:互联网 

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();
        }
    }
}using System;

namespace CsStudy
{
 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: