Skip to main content

Posts

Showing posts from November, 2023

C# : Understanding Eager Loading, Lazy Loading, and Explicit Loading in Entity Framework

Introduction Entity Framework, a powerful Object-Relational Mapping (ORM) tool, provides developers with various loading strategies to retrieve and work with data efficiently. In this blog, we'll delve into three key loading techniques: Eager Loading, Lazy Loading, and Explicit Loading, each serving distinct purposes in optimizing data retrieval. Eager Loading Eager loading is a strategy where related data is loaded from the database along with the main entity.  This is particularly useful when you know in advance that certain related data will be needed, helping to minimize subsequent database trips. Let's have a look at example in Entity framework core var employees = context . Employees . Include ( e => e . Department ) . ToList ( ) ; C# Copy In this example, the .Include() method ensures that the Department related to each Employee is loaded eagerly. Lazy Loading Lazy loading defers the loading of related data until the mom...

C# 11.0 : List Patterns

Introduction In C# 11, a game-changing feature was introduced – List Patterns. These patterns enable you to effortlessly match an array or a list with a sequence of elements. Let's explore the three distinct list pattern matching techniques Discard Pattern The Discard Pattern is useful when you know the length of the sequence. For instance, matching an array of five integers against another array with the same values var ids = new [ ] { 1 , 2 , 3 , 4 , 5 } ; if ( ids is [ 1 , 2 , 3 , 4 , 5 ] ) { Console . WriteLine ( "Its matched" ) ; } C# Copy To match regardless of the values, you can use the underscore (_) if ( ids is [ _ , _ , _ , _ , _ ] ) { Console . WriteLine ( "Its matched" ) ; } C# Copy Range Pattern When you're unsure about the sequence length, the Range Pattern comes into play. Use two dots (..) to specify any number of elements. For instance if ( ids is [ 1 , .. ] ) { Console . WriteLine ( "...

C# : 12.0 : Collection expressions

Introduction Collection expressions is a new feature introduced in C# 12.0. Lot of us know about collections and very familiar with generic collections also. We know that in collections, lot of flavours like arrays, lists.. etc. Till C# 11.0 different collections used different syntaxes for initialization. Let us go through the old syntaxes which will be revision for us. In this article, will demo both arrays and list. Arrays are one of the collection , we rapidly use in many scenarios. Will have a look on different ways of array initializations // old way of initialization int[] array1 = new int[] { 1, 2, 3, }; int[] array2 = new [] {1, 2, 3, }; int[] array3 = { 1, 2, 3, }; In above code, initialized arrays in different ways till C# 11.0. Even in C# 12.0 also we can follow same way of initialization. But in new version, introduced new way of initialization // new way in collection expressions int[] array4 = [1, 2, 3,4]; In the same way will h...

C# : Throw vs Throw ex

Exception handling is a crucial aspect of robust software development, and in C#, the throw statement is a fundamental tool for managing exceptions. In this blog post, we'll explore the nuances of throw and throw ex, backed by real-world analogies and C# code snippets. The Purpose of Exception Handling The Safety Net in Software Development In the unpredictable world of software development, errors and unexpected scenarios are inevitable. Exception handling serves as the safety net, allowing developers to gracefully manage and recover from unexpected situations. The throw statement is a key player in this process. throw: Propagating Exceptions Unveiling throw The throw statement in C# is used to explicitly throw an exception. It signals to the runtime that an exceptional condition has occurred, and the responsibility is handed over to the closest enclosing catch block for further handling. Key Characteristics of throw Exception Propagation: throw propagates an exception up the cal...

C# : 12.0 : Primary constructor

Introduction In C# 12.0, the introduction of the "Primary Constructor" simplifies the constructor declaration process. Before delving into this concept, let's revisit constructors. A constructor is a special method in a class with the same name as the class itself. It's possible to have multiple constructors through a technique called constructor overloading.  By default, if no constructors are explicitly defined, the C# compiler generates a default constructor for each class. Now, in C# 12.0, the term "Primary Constructor" refers to a more streamlined way of declaring constructors. This feature enhances the clarity and conciseness of constructor declarations in C# code. Lets see an simple example code, which will be known to everyone. public class Version { private int _value ; private string _name ; public Version ( int value , string name ) { _name = name ; _value = value ; } public string Ve...