Casting Between Custom Types in C#

This is a follow up from the article on Casting Custom Types To C# Built-In Types.

In the previous article we looked at how we would take a custom object like a Class or a Struct and cast it to a C# built-in like a bool. In this article we will look at how to cast from one custom type to another.

What we will be exploring here is much like what AutoMapper does.

Casting Custom Types To C# Built-In Types

Quick Quiz

What does the code below do?

CustomObject c1 = new CustomObject()
bool variableName = Convert.ToBoolean(c1);

//1. variableName is false
//2. runtime exception
//3. It depends

If you’re not sure what the answer is, read on - all will be clear.

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now