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 as a List 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, After 15 years of building web apps, I just recently discovered Vertical Slice Architecture. I know. I’m late to the party. But wow, this approach really changed how I think about structuring Blazor projects. So I decided to show you a simple, real-world example of how to use Vertical Slice Architecture in a Blazor Server app. 🎥 Watch the full tutorial now: In this new tutorial, you'll learn: ✅ How to structure your app with feature folders ✅ How to avoid overcomplicating things...

video preview

Hey Reader, In my last videos, we built a solid CRUD API using Vertical Slice Architecture, Minimal APIs, and Carter - super clean and modern. But there’s still one problem... 👉 What happens when a user sends empty or invalid data? In this new YouTube tutorial, I’ll show you how to fix that with: And yep, the full source code is free to download. Just check the link in the video description. If you’ve been enjoying this Vertical Slice series, this is a must-watch. It’s a simple upgrade that...

video preview

Hey Reader, If you're building apps with Blazor, you've probably asked yourself this: 👉 Should I use SSR, Blazor Server, or WebAssembly? It’s a common question - and I get it a lot. So I made a brand-new YouTube video walking you through how to choose the best one for your app. 🎥 Watch it here: If you're building a Blazor app or planning to do so soon, this should help clear up a lot of confusion. Let me know what you think, and feel free to reply if you have any questions. Take care, Patrick...