The Real Difference Between Add, TryAdd, and TryAddEnumerable in .NET

📰 Dev.to · Girgis Adel

Learn the differences between Add, TryAdd, and TryAddEnumerable in .NET DI registration and how to use them correctly

intermediate Published 8 Feb 2026
Action Steps
  1. Register a service using Add to ensure it is always added to the DI container
  2. Use TryAdd to add a service only if it has not been registered before
  3. Apply TryAddEnumerable to register multiple services of the same type
  4. Test the differences between Add, TryAdd, and TryAddEnumerable using a sample .NET application
  5. Configure the DI container to use the correct registration method based on the application's requirements
Who Needs to Know This

Backend developers and software engineers working with .NET will benefit from understanding the nuances of DI registration to write more efficient and scalable code. This knowledge is crucial for designing and implementing robust dependency injection systems.

Key Insight

💡 TryAdd and TryAddEnumerable are used to avoid duplicate registrations, while Add will always register a service even if it already exists

Share This
🚀 Understand the differences between Add, TryAdd, and TryAddEnumerable in .NET DI registration to write more efficient code! #dotnet #di #dependencyinjection

Key Takeaways

Learn the differences between Add, TryAdd, and TryAddEnumerable in .NET DI registration and how to use them correctly

Full Article

A lot of developers think DI registration is simple: services.AddScoped<IMyService,...
Read full article → ← Back to Reads