© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•16mo ago•
14 replies
pseudo

Any case where `TError` can't be boxed in example?

So I have the following:
public class ParseResult<TResult, TError>
{
    private readonly TResult? _result;
    private readonly TError? _error;

    private ParseResult(TResult? result, TError? error)
    {
        _result = result;
        _error = error;
    }

    public static ParseResult<TResult, TError> CreatedResult(TResult result)
        => new ParseResult<TResult, TError>(result, null);
}
public class ParseResult<TResult, TError>
{
    private readonly TResult? _result;
    private readonly TError? _error;

    private ParseResult(TResult? result, TError? error)
    {
        _result = result;
        _error = error;
    }

    public static ParseResult<TResult, TError> CreatedResult(TResult result)
        => new ParseResult<TResult, TError>(result, null);
}

But am getting an error on last line passing
null
null
in, I can see it resolves if i add a
class
class
constraint to the generic arg, or if i give
default
default
, but confused why it cant infer that
TError?
TError?
is always gonna be boxed, i.e. if i had
int
int
as
TError
TError
surely it can work out
Nullable<int>
Nullable<int>
from
TError?
TError?
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

Can't use | operator in case?
C#CC# / help
2y ago
✅ where DSA can be applied?
C#CC# / help
2y ago
❔ SQL Server | Can i use 'Case When' in Where Clause?
C#CC# / help
4y ago
DeleteBehavior can't be cascade in join table
C#CC# / help
2y ago