Lazy loading and eager loading are two contrasting approaches in managing how and when data is loaded into memory in programming, particularly relevant in the context of databases, data processing, and UI rendering. Lazy Loading In lazy loading, data is loaded only when it is required. This can enhance application performance and reduce memory usage, especially when dealing with large datasets or complex objects. For example, consider an e-commerce application that displays product details. With lazy loading, the detailed data for a product (like high-resolution images or reviews) is only loaded when a user selects that product, rather than loading…
Advanced C# Tips: Avoid Excessive Inlining of Methods
Inlining methods is a technique where the compiler replaces a method call with the actual method body's code. While inlining can speed up your program by eliminating the overhead of a method call, excessive inlining can lead to problems. This approach needs to be used carefully since you need to balance performance gains with potential drawbacks. My suggestion will be my typical suggestion, do not use unless it is really necessary. Method inlining can reduce execution time, especially in certain loops or highly repetitive code paths since it removes the overhead of the method call itself. However, excessive inlining can…
Advanced C# Tips: Using Array Segments Instead of Copying Arrays
Even though it is not a very popular C# tool, the ArraySegment<T> is quite old and it was introduced in .NET Framework 2.0, which was released in 2005. It is available for developers to utilize in their applications to improve performance when dealing with partial arrays. Technically, an ArraySegment is a struct, in other words, it is a value type in C#. When you instantiate an ArraySegment<T>, you are creating a lightweight object that doesn't have the overhead of a class, such as a reference type's heap allocation etc. The ArraySegment<T> struct internally holds three pieces of data: a reference…
Advanced C# Tips: Reuse Objects Where Possible
Reusing objects in C# is a strategy that aligns with the principles of efficient memory management and application performance optimization. This concept is important in environments where memory resources are limited or when the cost of object creation is high. For instance mobile applications, embedded systems, or server applications handling multiple concurrent requests count as typical examples. The underlying technical reason for object reuse is to reduce the overhead related to the memory allocation and garbage collection. Every time an object is created, the .NET runtime needs to allocate space on the managed heap. As objects accumulate, the garbage collector…
Advanced C# Tips: Utilize ArrayPool for Frequent Array Allocations
ArrayPool<T> in C# is a very useful but a less known feature. It has been around since .NET 2.0 but has never been popular. In order to understand ArrayPool, we can use the analogy of a communal bike-sharing system. Just as you can pick up a bike when you need it and return it when you're done. ArrayPool<T> lets you borrow and return arrays, reducing the need to constantly create new ones. This is useful when working with large arrays or in scenarios where arrays are frequently created and discarded. For instance high-performance computing, data processing, or handling large datasets…
Advanced C# Tips: Consider Leveraging Bitwise Operations for Simple Calculations If Possible
Bitwise operators in C# are special tools that let you work directly with the individual bits of a number's binary representation. If you are reading this blog post, I assume you also know that in computing world, numbers are actually stored as sequences of bits. Bitwise operators allow you to perform operations on these sequences bit by bit. They are like the switches that can flip the bits in certain patterns. They allow for a variety of operations such as flipping bits on and off, shifting them left or right, or comparing two values bit by bit. However we can't…
Advanced C# Tips: Use Exceptions Wisely
Exceptions in C# are definitely a great facility but we need to use them wisely. We should leave the least possible probabilities of throwing an exception since exceptions come with a cost. Exceptions simply signal that something has gone wrong in your code. The purpose of an exception is to allow your program to understand and respond unexpected problems like cleaning up resources or notifying the user. However making decisions about the program's logic is not among the primary purposes of exceptions. Even though I am talking through C#, this is true for all programming languages having exceptions. So, using…
Advanced C# Tips: Prefer Value Types Over Reference Types
In C#, understanding the difference between value types and reference types is a fundamental topic when writing efficient and performant code. The choice between using a value type (like a struct) or a reference type (like a class) can have significant implications on both memory usage and performance. The technical reasoning behind preferring value types over reference types lies in how they are managed in memory. Reference types are stored on the managed heap and come with an overhead of garbage collection. It can affect performance especially if there are many allocations and deallocations. Additionally, accessing data through a reference…
Advanced C# Tips: Prefer Structs for Immutable Data
Before diving into topic, I want to begin with clarifying the terminology. Immutable data refers to data whose state cannot be modified after it has been created. Once an immutable object is created, its internal state remains constant throughout its lifetime. Any attempt to modify the object's state results in the creation of a new object with the updated state, leaving the original object unchanged. In C#, you can define your data using either classes or structs. The choice between a class and a struct can influence the behaviour and performance of your application. Classes are reference types and structs…
Advanced C# Tips: Use ‘in’ Parameter Modifier for Large Value Types
In C#, the in parameter modifier is relatively a recent addition. It added in C# 7.2. The main point of in keyword is to pass a large value type to a method without copying the entire structure. Value types in C# are typically passed by value. This means that the entire thing is duplicated and handed off to the method. This may not be a big deal for small value types like int or bool. However, for larger value types, it can be quite expensive in terms of performance because it takes time and memory to take care of these…
Azure Active Directory Login with React, with C# or Java Backend
Keeping things secure is the important part of any application. As entire internet going to cloud, Azure Active Directory (AD) is one of the best possible among one of two popular options. It helps manage who can use your app and what they can do. Let's see how to set up Azure AD for a C# and Java Spring Boot backend, and a React frontend to make sure only the right people get in. First, go to the Azure portal and make a new Azure AD tenant if you don't have one yet. Then, register your app in Azure AD.…
Unlock Faster Performance: Advanced Caching Tactics for .NET Core
In software development, performance optimization is a crucial topic and one of the most effective ways to improve the performance is caching. However, caching is a double edged sword we need to very careful about when and how to use caching. So, we need to use right strategy in right place. Caching can be implemented at different levels, including memory, network, and CDN (Content Delivery Network) levels. Each level has a unique purpose and is suitable for different use cases. In this post, we will focus on memory-level caching which is simply storing the data in the memory of the…
Containerizing a .NET Application with Docker – Step by Step Guide
Docker is a powerful tool that allows developers to package applications into containers. These containers can run on any system that has Docker installed, making it easier to deploy and run applications consistently across different environments. Containers are like lightweight, portable packages for your application, including everything it needs to run: code, runtime, libraries, and settings. I assume that you have already know what Docker is and also you have .NET and Docker installed on your machine. Don't worry, just download and then next + next. For more details and official documentation, you can visit Docker's official website and for…
Remote Caching in Scaled-Out Cloud Applications: A Case Study with Redis, C# and Python
A few weeks ago, we were discussing a problem with my team mates and a colleague raised a question about a very basic distributed remote caching problem which was about an application scaled out across multiple instances, managing how data is cached can become a significant challenge. Let me take the shortcut and directly jump into the problem. Consider a scenario where you have multiple instances doing the same job and you need to cache the result of a function which takes about 30 seconds. This function's result needs to be cached to avoid redundant executions across different instances. The…
Using SignalR with Angular
In a previous blog post, we discussed how to create real-time web applications using SignalR and React. If you have already gone through that, you would know the magic of SignalR in pushing instant updates, allowing users to receive data in real-time. In this post, we're going to apply the same concept but with Angular. Setting Up the Server with SignalR Just as a quick refresher, SignalR provides an easy method to communicate between the server and the client in real-time. We start by installing the appropriate NuGet package: Install-Package Microsoft.AspNet.SignalR The hub, as described before, acts as the communication…
On Azure Functions and Serverless
There's no shortage of tools and technologies to make developers' lives easier In the world of cloud computing, and one such offering comes from Microsoft which is Azure Functions. Here, we'll unravel the magic behind Azure Functions and explore why it's becoming a favourite for certain architectures. At its core, Azure Functions is a serverless compute service. "Serverless" might sound misleading, as there definitely are servers involved; however, the idea is that the responsibility of server management, scaling, and infrastructure concerns are taken away from the developer. This allows us to focus on writing and deploying our code. Azure Functions…