在C#中如果定义显式/隐式转换是不是没有位置的要求,只要在任一类中定义了转换就可以?
例如代码1和代码2木有区别咯?
代码1
public class ConvClass1
{
public int val;
public static implicit operator ConvClass2(ConvClass1 op1)
{
ConvClass2 returnVal = new ConvClass2();
returnVal.val = op1.val;
return returnVal;
}
public static implicit operator ConvClass1(ConvClass2 op1)
{
ConvClass1 returnVal = new ConvClass1();
checked { returnVal.val = (int)op1.val; }
return returnVal;
}
}
public class ConvClass2
{
public double val;
}
代码2
public class ConvClass1
{
public int val;
public static implicit operator ConvClass2(ConvClass1 op1)
{
ConvClass2 returnVal = new ConvClass2();
returnVal.val = op1.val;
return returnVal;
}
}
public class ConvClass2
{
public double val;
public static implicit operator ConvClass1(ConvClass2 op1)
{
ConvClass1 returnVal = new ConvClass1();
checked { returnVal.val = (int)op1.val; }
return returnVal;
}
}
应该没区别吧