© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
12 replies
Dyad

❔ Maybe Disposable Object

I want to have a function that returns an HttpClient or an error. The function would take in JSON API user credentials and return an HttpClient with the Base Address and Authorization Headers set if the credentials are valid, otherwise an error message. When consuming this function I was going to use a using statement:

using(var client = MakeNewClient(username, password))
{
}

or...

var clientOrError = MakeNewClient(username, password);
if(client.Error != null)
return;

using(clientOrError.client)
{
}

What should the return type of MakeNewClient() be in order to surface the error and still be able to use it in a using statement? Or is my approach to this wrong all-together?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,828Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

ValueType disposable scope
C#CC# / help
5mo ago
✅ Disposable class level property
C#CC# / help
14mo ago
Maybe the case for object pooling?
C#CC# / help
11mo ago
✅ is it a good disposable class?
C#CC# / help
3y ago