← Back
Milan Jovanovic November 20, 2026 11m

I Replaced My Result Class With One Line of C# 15

Read full transcript 8 segments
  1. With the release of C 15, we are finally getting native support for union types. What I'm wondering is if this is finally going to make functional programming, monads, and the result pattern more mainstream with .NET developers. In this video, I want to talk about how union types change the result pattern implementation that I've been using for years and in my opinion improve on it. I'll first do a quick intro on union types just so that we can understand what they are. And here's an example of a very simple union type defining a GID or an error. And this is what a union type is. A union represents a value from a fixed set of types. And in this case, the types are going to be a GID or an error class which I have defined inside of this codebase. If I just jump over to it real quick, it's a simple record with a code and a description. And I'm using it to define a failure case with the result pattern. and it can hold one of the predefined errors like the apartment errors or the booking errors. Now back to our demo. How do we actually use this union type? I'll create a static class just to define a small helper method. So let's say we have two cases a success case which can hold a new GUID instance and we create it by simply assigning a value of a GUID to our union type. And we can do the same for the failure case and let's say I use apartment errors not found for this. So notice that I didn't have to call some sort of constructor. I

  2. just create my value and assign it to the union instance. So now this instance can be either one or the other of my predefined types. And you can also define more than two types obviously. And this is just going to make your union more expressive but also more complex. Now what a union is behind the scenes is it's turned into a strct and it holds the actual value inside of an object. So if I take my success union instance and we try to see what it has exposed to us, you'll see equals get hash code get type to string and value. And the value is what holds our GUID instance or the error. And because this is an object, it means our GID will be boxed when converting it to our union instance. So we can access the value just like any other property. But this is not how we are actually supposed to be using our unions. you are supposed to use a switch expression on them. And here's an example of what that might look like. I'll accept this completion, but we're going to get rid of the default branch because we don't really need it. In order to make our switch expression exhaustive, we just need to define a case for all the types that make up our union. And in this case, it's going to be either a grid or an error type. And you can just call this with your union instances. and it's going to print out the respective values in the console. So that's a quick primer on union types. Now, how do we actually use them with the result pattern? Well,

  3. I've got a simple application here which is going to execute a command handler. And I have a reserve booking use case. Now, what I'm doing here is setting up my handler with a validation decorator as I want to show you how this also affects generic decorators. I've got a couple of commands. Some of these can succeed, some of them can fail. We process the command and we print out the output. Inside of this respond method, we're going to handle the result instance that the handler returns. And based on if it's success or failure result, we're going to return different values. Now, if I want to quickly run this, I can open up my terminal, type in net run, and you'll see that this is going to be compiled using .NET 11, mind you, as this is a C# 15 feature. and we'll get a couple of values printed out to the console depending on which case we hit and our handler. So it can either be the happy path returning do one created or a4 not found 409 conflict or 400 bad request. So I'm essentially mocking an API response. Now let's look at the handler. Very typical use of a result pattern. We return a result of a grid and we implement a handler interface which just defines a generic response. Inside of the handler, we call our repository to get an apartment. We check if it's null. So that maps to a not found error. We check some business rule and this maps to another error. If we trigger the case and otherwise we can create our booking, add it to our mock database and return the booking ID from

  4. the handler which constitutes our happy path. The validation decorator just wraps our inner handler. So we first validate the command request object and if we get back an error we return a failure result otherwise we just call the inner handler and return whatever it returned. And then finally let's look at the result class which is pretty complex. We got a result type and also a generic result type that holds a value inside as my implementation at least needed two classes to define these. We have success or failure properties. some helper methods for constructing these values. And you will also commonly see implicit operators and a bunch of other helper methods in most implementations out there. Now, how does this change with union types? Well, let's see. I'm going to replace the class here with a union. And you can make a union type generic. So, our types can be either a T, which can hold any value, or let's say an error, which will represent our failure case. And with this one line of code, I'm going to comment out everything else. Then this is now my new result type. Now if I try to build my solution by running net build, we're going to see that we get a bunch of compilation errors. And most of these have to do with the previous cases where we were using the result pattern. So let's try to fix them one by one. I'll start from the reserve booking handler.

  5. Notice that the handle method is fine. we return a result wrapping our GUID. Because our union type is generic, we can do this without any issues. The problem here is the conversion to an error. However, you'll see that the solution for this is incredibly simple. We just return the error type and C can convert this to our union definition without any issues. We do the same in the second case. And you'll see that the compiler errors now disappear and our happy path is fine because returning a good value maps to our union type without any issues. Practically speaking, nothing changes in our handler and in fact it becomes simpler as we got rid of some helper method calls. The next step is going to be our validation decorator where we are calling a now non-existent failure method. We just return the error instance and this maps to our union type without any issues. So again a simplification and the last place where we have some errors is the program file mainly in the respond method which is supposed to represent our minimal API where we process the result and you'll see that this is actually the most interesting part. Look at what we had to do to process our result class instance. We had to check if it was a success. So somebody had to remember to write an if statement and then return the happy path response. and we have to process all of the errors inside of a switch expression for example along with a default case like we have here. So how does this change

  6. with a union type? Well remember that we're supposed to use a switch expression on it. So I'm going to say result switch. Then we have to handle exhaustively the types that make up our union. So the types can be either a GUID containing the booking ID and we're going to return 2011 created in this case. Let me copy the value from above and I'm going to pass in the booking ID that we capture as part of this pattern match. Now I can get rid of this if check here and this takes care of my happy path. Now the rest of these are going to be our failure cases and we have a couple of them. So let's first handle the default case. We handle this just with an error and we also have to capture the actual value so that we can use it to pass along the description. Now what about the examples here where we have to match on a specific error code? How can we do this with union types? Because the error codes themselves are constant values. We can do a pattern match on our union types instance. So I can say if this is an error where the code is my apartment not found case. So something like this then I can return this specific error and I'm going to replace this with the error instance. So now we can look at the two side by side. Previously we would be matching on the result error code and now we have to do a bit more juggling around to get this to work but we end up achieving the same result. So I'm going

  7. to copy this branch two more times and I'm going to replace the error codes that we are matching on and our final switch expression is going to look something like this. The rest of our code remains unchanged as it's still consuming the result of good. Now a couple of things to note here. First, this implementation is better because the compiler forces you to process all of the possible options. You have to handle both the happy path and the failure paths with a result type implemented as a union. If I go ahead and do a net build, you'll see that the build passes. And if I do a net run, you'll see that we get our four cases back with the result pattern using the union types in C# 15. Some drawbacks to note with this particular implementation is the compiler can't know about all of the possible error cases that you might have. So if I go ahead and add another error code, for example, booking not found, our switch expression over the union type doesn't have a way to know about this existence because we are doing pattern matching on the error code. Now there is a way to fix this, but I'm going to leave that for a future video and I also want to make another video discussing the performance implications of this new implementation of the result pattern. Overall, in my opinion, this is an improvement over the old implementation as it now forces you to handle all of the possible cases. And it also gives you a way to express all of the failure types. But as I said, I'll cover that in a future video. And

  8. just a little hint for you. What if we made all of our errors also types and put them into a union? This is something that you can do, but I'll let you explore this on your own if you want to give it a try. So, there you have it. We now have union types in C# 15 and I can finally use it to make the result pattern great again. Whether it's too little too late as agents are now writing most of production code out there. I leave that up to you. I'm personally happy that Microsoft finally shipped this and I am going to instruct my agents to start using it. If you enjoyed this video, go ahead and smash the like button to let me know. Thanks a lot for watching and until next time, stay awesome.

No summary available yet.

View original episode ↗