Converting object of a class to of another one

前端 未结 7 695
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 08:33

I have two classes which have are nearly equal except the data types stored in them. One class contains all double values while other contains all float values.



        
7条回答
  •  时光说笑
    2020-12-28 08:43

    You can use Conversion Operators to achieve this.

    Fr example:

    struct FloatClass
    {
        public FloatClass(DoubleClass dClass) {
            //conversion...
        }
        ... 
        public static explicit operator FloatClass(DoubleClass dClass) 
        {
            FloatClassd = new FloatClass(dClass);  // explicit conversion
    
            return d;
        }
    }
    
    
    var convertedObject = (FloatClass)doubleObject;
    

提交回复
热议问题