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

Hey Reader, Last week, I tested GPT-5 Agent Mode on .NET and Blazor apps, and the results were surprising. This week, I went a step further. I used GitHub Copilot inside Visual Studio and asked it to create real .NET and Blazor projects for me. Here’s what happened: Copilot generated a working calculator app in C#. It built a Web API that responded with “Hello GPT-5 Agents.” It even created a Blazor app with extra features like a reset button, a plus five counter, and a weather API. Were...

video preview

Hey Reader, I just published a new video where I put GPT-5’s Agent Mode to the test. 🚀 Instead of asking it for something simple, I went all in: A .NET 9 console app calculator A Web API with endpoints and docs A Blazor Server app with extra counter buttons And even a Blazor app that calls a public weather API 🎥 Watch it now: In the video, I didn’t just type “make me an app” and hope for the best. I gave GPT-5 very specific prompts, like: “Please create a .NET 9 console application that works...

Hey Reader, I’m back! 🎉 I’ve been on parental leave for the past 2 months, enjoying time with the family. Now I’m diving back into videos and here’s the first one! 🎥 Watch it now: Here’s the TL;DR if you can’t click right now: AI can already write code, fix bugs, and explain complex concepts. So… are we doomed? No. AI isn’t replacing developers anytime soon. But: Developers who use AI will replace those who don’t. The best devs are using GPT to: Skip repetitive work Prototype faster Learn...