© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
6 replies
CrosRoad95

❔ Garbage collection question

I see in many libraries examples where in DI ".AddScoped<>", ".AddTransient<>" is used instead of ".AddSingleton<>", example in fluent assertions example says:
        services.AddScoped<IValidator<User>, UserValidator>();
        services.AddScoped<IValidator<User>, UserValidator>();
and i wonder why? this class instance is sorto "read only" so it could be singleton? And here's my question regarding that, because singleton will create long lived instanced meanwhile the other two will live for let's say 100ms, they will be regularly cleared, less often reach further generation. I know that not all singletons are used at all time, they will live in memory for long time and cause extra pressure on garbage colletor.
What do you think? is it better to use Transient/Scoped even when singleton make sense from memory point of view?
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

Garbage Collection Questions
C#CC# / help
3y ago
❔ Release mode garbage collection is much longer than debug mode
C#CC# / help
4y ago
Garbage collector
C#CC# / help
17mo ago
[Solved] COM Interop, Garbage Collection, and Runtime Callable Wrapper object management [Answered]
C#CC# / help
4y ago