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, You may have seen the recent blog post from Jimmy Bogard (creator of MediatR and AutoMapper) announcing that these libraries are going commercial. And to be clear, I fully support that. Time is money, and supporting your family always comes first. BUT… In most of my projects, I don’t actually use everything MediatR offers. I usually just need a small part of it, the classic "send a request, get a response" pattern. So I thought: why not build a simple version myself? šŸ‘‰ That’s...

video preview

Hey Reader, In my last video, I showed you how to build a CRUD app using Vertical Slice Architecture and controllers in .NET 9. But… What if you could do the same thing - cleaner, simpler, and without all that controller clutter? šŸ‘‰ That’s exactly what I show you in my brand-new video: In this one, you’ll learn how to: Use Minimal APIs instead of controllers Keep your Program.cs neat and tidy Stay fully organized with the Vertical Slice Architecture Work with Carter, a super helpful library...

video preview

Hey Reader, If you’ve ever built a simple CRUD app with a layered architecture… you might be making things harder than they need to be. In my newest YouTube tutorial, I show you how to build a scalable CRUD app using something super clean and refreshing: šŸ‘‰ Vertical Slice Architecture in .NET 9 We’ll build a full Video Game API, step-by-step, with: .NET 9 Web API + Entity Framework Core Code-first migrations Controllers (yep, still using those!) The Mediator pattern for cleaner code And, of...