C#中自定义显式/隐式转换的问题

在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;
}

}
慕森卡
浏览 336回答 1
1回答

30秒到达战场

应该没区别吧
打开App,查看更多内容
随时随地看视频慕课网APP