Ranjithkumar October 22, 2023 0

Lookup DI services using keys in .NET 8

One of the new features in .NET 8 is the ability to dynamically lookup DI services using keys. This can be a useful way to decouple your code from the concrete implementation of a service. For example, let’s say you have a service that sends notifications. You could have a concrete implementation for each type of notification, such as EmailNotificationService and SmsNotificationService. Here is an example of how to register keyed DI services in ASP.NET Core: In the past, you would have to inject the specific service you needed into your constructor. For example, if you wanted to send an…

Ranjithkumar October 21, 2023 0

New System.Collections.Frozen namespace in .NET 8

The .NET 8 release introduces a new namespace, System.Collections.Frozen, which provides two new immutable collection types: FrozenDictionary<TKey, TValue> and FrozenSet<T> These types are designed for scenarios where collections are created infrequently but are used frequently at runtime. They offer excellent lookup performance and are ideal for cases where a collection is created once, potentially at the startup of an application, and is used throughout the remainder of the life of the application. Benefits of using frozen collections Examples of using frozen collections Example 1: Using FrozenDictionary to store configuration data In this example, the configuration variable is a FrozenDictionary that…

Ranjithkumar October 16, 2023 0

Do not throw Exceptions in C#

Exception handling is a crucial aspect of software development, and the concern about its performance implications is valid. In this blog post, we’ll explore the idea of avoiding exceptions to control the flow of code in C# and opting for alternative approaches to achieve the same results more efficiently. We’ll delve into why exceptions can be costly, explain what happens when an exception occurs, and provide C# examples of how to use defaults for better control. The Cost of Exceptions Exceptions are a powerful tool in C#, but they come with a performance cost. When an exception is thrown, the…