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, Ever tried to choose the perfect weapon for each creature in a dark, spooky forest? Silver bullets for werewolves, garlic for vampires, chainsaws for zombies... but what if you had to pick your weapon before entering the forest? Just like that forest adventure, dependency injection in C# can leave you stuck with the wrong tool when you need it most - unless you know about the Factory Pattern! Today, I’m breaking down how the Factory Pattern solves real-world dependency injection...

video preview

Hey Reader, In today’s tutorial, I’ll walk you through how to use Dapper, a high-performance micro ORM (object-relational mapper) with a .NET 8 Web API and SQL Server. We’ll cover everything you need to know to build a simple CRUD (Create, Read, Update, Delete) application. Watch on YouTube 📺 Check out the full tutorial on YouTube to see everything in action 👇 What We’ll Be Doing We’re going to work with a simple video game database to start. You’ll learn how to: Read all games or a specific...

video preview

Hey Reader, Today, I show you how combining Blazor and JavaScript can give you the best of both worlds. Let’s dive into how you can easily mix the power of these two technologies in your projects! Watch on YouTube 📺 Check out the full tutorial on YouTube to see everything in action 👇 A Simple Example: Console Logging in Blazor Imagine you’re working on a Blazor Server application, and you want to log the current count (of the Counter page) to the console. Your first thought might be to use...