C#: Custom casting to a value type

后端 未结 5 2029
不知归路
不知归路 2021-02-14 10:04

Is it possible to cast a custom class to a value type?

Here\'s an example:

var x = new Foo();
var y = (int) x; //Does not compile 

Is i

5条回答
  •  情歌与酒
    2021-02-14 10:45

    I would suggest you implement the IConvertible interface as that is designed to handle this. See http://msdn.microsoft.com/en-us/library/system.iconvertible.aspx

提交回复
热议问题