I’ve tried googling this and am also using the search feature on this site, but none of the answers I’ve found answer my question.
In Visual Basic, if I wanted an easy way to convert from one of my classes to another custom class, I can define the CType operator to define how to convert from one class to the other. I can then call CType( fromObject, toNewType) to do the conversion or in C# I guess you can just do a simple cast.
However, in C#, how do you define how the actual cast will be handled from one custom class to another to another custom class (like you can in Visual Basic using the CType operator)?
You could define a custom cast using the explicit keyword:
…but consider not doing that. It’ll confuse the folks who will have to maintain your software down the line. Instead, just define a constructor for your target class, taking an instance of your source class as an argument: