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 Single Object CastingWe 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 List of Objects CastingNow, letās get a list of characters: Hereās the problem: You can't return a How to Fix ItTo fix this, we use LINQās This code goes through each Summary
See it in ActionCheck out the full tutorial on YouTube to see this in action š ConclusionI 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:
PPS: Would you like to sponsor this newsletter? I'm just an email away. Have an awesome weekend! š |
Become a .NET & Blazor expert with weekly tutorials featuring best practices and the latest improvements, right in your inbox.
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...
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...
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...