Posts

🎓 HackerRank Practice: Student Marks Manager using C# Lists

Working with Lists is one of the most common exercises in coding challenges. Lists in C# are dynamic collections that allow adding, removing, and inserting elements easily. Let’s solve a simple HackerRank-style problem step by step. 📌 Problem Statement: Student Marks Manager You are given student marks. Perform operations using a List<int> . Input 5 10 20 30 40 50 3 Add 60 Remove 20 Insert 25 2 Output 10 25 30 40 50 60 Explanation Start: [10, 20, 30, 40, 50] Add 60 → [10, 20, 30, 40, 50, 60] Remove 20 → [10, 30, 40, 50, 60] Insert 25 at index 2 → [10, 25, 30, 40, 50, 60] ✅ This tests: Index-based access Add / Remove operations Insert at a given index 🛠️ Solution in C# using System; using System.Collections.Generic; public class Program { public static void Main() { // Input: number of students int n = int.Parse(Console.ReadLine()); // Input: student marks string[] input = Console.ReadLin...

📌 Indexes in EF Core

Indexes are one of the most powerful tools for query performance tuning. In Entity Framework Core (EF Core), indexes help: ⚡ Speed up lookups, filtering, and sorting 🔒 Enforce uniqueness 🏢 Support multi-tenant and domain-specific rules This guide walks through everything you need to know — from the basics to advanced scenarios — with real-world code samples and best practices. ✅ What is an Index in EF Core? An index is a database object that allows the database to locate rows faster. Use .HasIndex() → to create indexes via Fluent API Use .IsUnique() → to enforce uniqueness EF Core automatically creates indexes for foreign key columns ✅ Defining Indexes in EF Core Type Description Basic Index Creates a non-unique index on the Name column. Unique Index Ensures no two rows share the same Sku (enforced at the DB level). Composite Index (Multi-Column) Used when queries filter on multiple fields together. Example: WHER...

🔑 Keys in EF Core

✅ 1. Primary Key (PK) Definition: Uniquely identifies each entity. Default Convention: Property named Id or <EntityName>Id . Explicit Definition: Data Annotation: [Key] public int ProductId { get; set; } Fluent API: modelBuilder.Entity<Product>() .HasKey(p => p.ProductId); ✅ 2. Composite Key modelBuilder.Entity<OrderDetail>() .HasKey(od => new { od.OrderId, od.ProductId }); ⚠️ Must be defined using Fluent API (Data Annotations don’t support composite keys). ✅ 3. Alternate Key modelBuilder.Entity<Product>() .HasAlternateKey(p => p.Sku); ✅ Primary Key vs Alternate Key Feature Primary Key Alternate Key Uniquely identifies? ✅ Yes ✅ Yes Required? ✅ Yes ❌ Optional Can be used as FK? ✅ Yes ✅ Yes (with .HasPrincipalKey ) ✅ 4. Foreign Key (FK) modelBuilder.Entity<Employee...

ASP.NET Core MVC Folder Structure & Program.cs Explained

ASP.NET Core MVC Folder Structure & Program.cs Explained ASP.NET Core MVC Folder Structure & Program.cs Explained Explore how your ASP.NET Core MVC application is organized and how Program.cs bootstraps it all. Table of Contents: Why Folder Structure Matters Folder-by-Folder Breakdown Program.cs and Middleware Quick Reference Table Why Folder Structure Matters A clean project structure makes your MVC application easier to scale, test, and maintain. In this post, we’ll explore each key folder and show you how Program.cs ties it all together. ASP.NET Core Folder Breakdown Folder/File Purpose /Controllers Holds controller classes that handle HTTP requests. /Models ...

ASP.NET Core MVC Request Lifecycle – From Browser to View

🔁 ASP.NET Core MVC Request Lifecycle – From Browser to View 🔁 What Happens When a Request Comes from the Browser in ASP.NET Core MVC? Understand the journey of a request in ASP.NET Core MVC — from browser to controller, through services, and back as a rendered Razor view. 🌐 1. The Journey Begins: Browser Sends a Request Every user action — like clicking a link or submitting a form — sends an HTTP request to your ASP.NET Core MVC application. The request first hits the Kestrel Web Server and flows through the middleware pipeline . This pipeline is configured in Program.cs and typically looks like this: var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllersWithViews(); builder.Services.AddScoped<IProductService, ProductService>(); // Dependency Injection var app = builder.Build(); if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error");...

Composite Design Pattern in .NET

The  Composite pattern  allows you to treat  individual objects  and  groups of objects (composites)  in a  uniform way . It represents part-whole hierarchies so that client code can interact with both single and grouped items through a common interface.   Purpose To build complex tree-like structures where  leaf nodes (single elements)  and  composite nodes (groups)  can be treated the same way.   Key Characteristics Defines a  component interface  for both leaf and composite Leaf handles base operations Composite stores child components and delegates operations   Pros Simplifies  client code  by treating all components uniformly Supports  hierarchical  and  recursive  structures Easy to add new types of components   Cons Can make the system overly general Harder to  restrict certain operations  for leaf-only o...

Proxy Design Pattern in .NET

The Proxy pattern provides a placeholder or surrogate for another object. It controls access to the real object and can add extra behavior like caching, logging, lazy initialization , or security .   Purpose To control access to another object. It can be used to defer resource-heavy operations, restrict access, or wrap remote calls and security logic.   Key Characteristics Implements the same interface as the real object Delegates requests to the actual object Can add extra logic (e.g., access control, logging)   Pros Adds control, performance optimizations, or protection Supports lazy loading or remote access Keeps real object usage transparent to the client   Cons Introduces additional layers Can increase complexity Misuse may lead to tight coupling   Use Cases Security proxies for sensitive financial operations Virtual proxies to delay ...