implicit和explicit的基本使用

 

 class MyAge
    {
        public int Age { get; set; }

        public static implicit operator MyAge(int age)
        {
            return new MyAge() { Age = age };
        }
        public static explicit operator int(MyAge myAge)
        {
            return myAge.Age;
        }

    }

 

 MyAge myAge=new MyAge(){Age = 20};
            int i = (int)myAge;

            MyAge myAge1 = i;

 

posted @ 2013-09-09 15:43  瓜王  阅读(187)  评论(0编辑  收藏  举报