© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
2 replies
Esa

❔ Help with designing a functional container object

Hi,
I could use some input for a functional container/result object I'm working on. I want it (the struct itself) to never be able to be null or contain null, but this is a situation I'm not quite sure how to handle.
It's meant to return the result of invoking a function, but some functions will evaluate to null and I'm not sure how to model that. 🤔
I know that functions that directly accept a
T
T
value can throw an exception if that is null as it can be seen in compiletime, but in this case I'm not quite sure what should happen as I cannot check a
Func
Func
for null until it is actually ran and then it is too late.

public readonly struct Outcome<T> {
  private readonly T value;
  private readonly string errorMessage
  public readonly bool IsSuccess

  public static Outcome<T> Of(Func<T> function)
  {
      if (function == null)
          throw new ArgumentNullException(nameof(function));
  
      try
      {
          var value = function();
  
          if (value == null)
              return value == null ? Error("Value cannot be null!") : Success(value);
              // throw new ValueIsNullException("Value cannot be null!");
  
          return Success(value);
      }
      catch (Exception exception)
      {
          return Error(exception);
      }
  }
public readonly struct Outcome<T> {
  private readonly T value;
  private readonly string errorMessage
  public readonly bool IsSuccess

  public static Outcome<T> Of(Func<T> function)
  {
      if (function == null)
          throw new ArgumentNullException(nameof(function));
  
      try
      {
          var value = function();
  
          if (value == null)
              return value == null ? Error("Value cannot be null!") : Success(value);
              // throw new ValueIsNullException("Value cannot be null!");
  
          return Success(value);
      }
      catch (Exception exception)
      {
          return Error(exception);
      }
  }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Designing a configurable object in EF Core
C#CC# / help
17mo ago
Help designing classes for a game
C#CC# / help
2y ago
✅ I need help with designing my microservices
C#CC# / help
4mo ago
❔ Designing a Worker system for a game
C#CC# / help
4y ago