Why Your Cast Doesn’t Work in C#: A Simple Guide 💻


Hey Reader,

Today, I want to show you something important about casting objects in C#. We’ll talk about the difference between casting a single object and a list of objects.

What’s the Problem?

Imagine you have a game with characters. We have a Character class that implements an ICharacter interface. Here's what that looks like:

Single Object Casting

We have a method to get a single character by its ID. Here’s how it looks:

When we run this method, it works because C# knows that Character implements ICharacter, so it can cast it automatically.

List of Objects Casting

Now, let’s get a list of characters:

Here’s the problem: You can't return a List<Character> as a List<ICharacter> directly because the compiler doesn't know how to do that. We have to explicitly cast each Character to ICharacter using LINQ’s Select method.

How to Fix It

To fix this, we use LINQ’s Select method to cast each Character to ICharacter:

This code goes through each Character in the list and casts it to ICharacter.

Summary

  1. Single Object: C# can cast a single Character to ICharacter automatically.
  2. List of Objects: You need to manually cast each Character to ICharacter.

See it in Action

Check out the full tutorial on YouTube to see this in action 👇

video preview

Conclusion

I hope this helps you understand how to cast objects in C#. Remember, single objects can be cast implicitly, but lists need explicit casting.

Happy coding!

Take care,

Patrick

PS: Need help understanding .NET & Blazor? There are two ways I can help you with:

  1. Check out the .NET Web Academy, which provides masterclasses and a supportive community of like-minded developers.
  2. I'm open to coaching. If you need specific help, reply to this email and we'll figure something out.

PPS: Would you like to sponsor this newsletter? I'm just an email away. Have an awesome weekend! 🎉


Patrick God

Become a .NET & Blazor expert with weekly tutorials featuring best practices and the latest improvements, right in your inbox.

Read more from Patrick God
video preview

Hey Reader, You sit down to build an API, add some authentication, and think, “JWT should do the trick.” But then you wonder: What happens if my JWT gets stolen? How do I keep users logged in without compromising security? Sound familiar? In my latest video, I show you how to implement secure JWT authentication with short-lived tokens and refresh tokens in .NET. This setup minimizes risk while keeping the user experience seamless. No fluff - just practical, real-world guidance for building...

Hey Reader, Tired of factories and messy if-else logic in your projects? Since .NET 8, Keyed Services make it easy to handle multiple implementations. No more clutter - just clean, scalable code. In my new video, you’ll see how to dynamically pick services using a key and write code that’s easy to extend. It’s a fresh, modern approach to dependency injection that’ll have you rethinking how you design your APIs. 👉 Watch the video here Wishing you a fantastic start to the new year filled with...

video preview

Hey Reader, You sit down to write an API, and before you know it, you’re knee-deep in controllers, attributes, and boilerplate. Sound familiar? What if you could skip all that and still build a fully functional API - fast? That’s where Minimal APIs in .NET come in. They’re clean and lightweight and make building APIs feel like a breeze. In my latest video, I show you how to set up endpoints step by step and structure everything neatly so your code stays easy to read and scale. No fluff - just...