C# .NET Interview Questions Guide
C# .NET Interview Questions Guide
NET Questions
Created by Arman Merchant
Tags
📘Guide
Complete .NET & SQL Interview Prep
🔹 C# / .NET Questions
Q1. Why use an Interface?
Defines a contract without implementation.
Example:
C# / .NET Questions 1
Q3. What is Dependency Injection (DI)? Why use it?
Definition: Dependencies provided externally instead of created inside.
Example: IEmailSenderService .
[Link]<IEmailSenderService, EmailSenderService>();
Scoped
Example: DbContext .
[Link]<MyDbContext>();
Singleton
Example: ICacheService .
[Link]<ICacheService, MemoryCacheService>();
C# / .NET Questions 2
Creating per request → socket exhaustion.
Solution → IHttpClientFactory
Usage
[Link]<GitHubService>(client =>
{
[Link] = new Uri("[Link]
});
AzureAdService Example
C# / .NET Questions 3
}
Registration
Lifetime Choice
Q5. Middleware
Request pipeline
components: UseRouting , UseCors , UseAuthentication , UseAuthorization , UseSession .
Q7. CORS
Cross-Origin Resource Sharing → allows/blocks requests across domains.
Hashing: one-way.
C# / .NET Questions 4
Async = doesn’t create thread; frees thread during I/O.
Action<T>: void.
C# / .NET Questions 5
Q18. IEnumerable vs IQueryable (Deep Dive)
IEnumerable
Works in-memory.
IQueryable
Q20. JWT
Client sends token → server validates signature & claims.
🔹 SQL Questions
C# / .NET Questions 6
Q1. Delete duplicates
WITH d AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Col1, Col2 ORDER BY (S
ELECT 1)) rn
FROM MyTable
)
DELETE FROM d WHERE rn > 1;
SELECT MAX(Salary)
FROM Employee
WHERE Salary < (SELECT MAX(Salary) FROM Employee);
C# / .NET Questions 7
HAVING COUNT(*) > 1;
Q8. ACID
Atomicity: all or none.
Isolation: no interference.
Q9. Indexing
Purpose: faster lookups.
Example:
C# / .NET Questions 8
Prevention: Parameterized queries, stored procs, input validation, least
privilege.
Avoid SELECT * .
Forms
Example
Normalized:
Customers(CustomerId, Name)
Orders(OrderId, CustomerId, OrderDate)
OrderItems(OrderItemId, OrderId, ProductId)
🔹 LINQ Questions
C# / .NET Questions 9
1️⃣ What is LINQ? → Query collections in C#.
2️⃣ Types → Objects, Entities, SQL, XML.
3️⃣ IEnumerable vs IQueryable → client vs server.
4️⃣ Deferred Execution → runs when enumerated.
5️⃣ Immediate Execution → , . ToList() Count()
class BankAccount {
private decimal balance;
public void Deposit(decimal amt) => balance += amt;
public decimal GetBalance() => balance;
}
Inheritance
Polymorphism
Abstraction
C# / .NET Questions 10
abstract class Vehicle { public abstract void Start(); }
class Car : Vehicle { public override void Start(){} }
🔹 C# / .NET Questions
Q1. Why use an Interface?
Defines a contract without implementation.
Example:
C# / .NET Questions 11
}
Example: IEmailSenderService .
[Link]<IEmailSenderService, EmailSenderService>();
Scoped
Example: DbContext .
[Link]<MyDbContext>();
Singleton
Example: ICacheService .
[Link]<ICacheService, MemoryCacheService>();
C# / .NET Questions 12
Q4. HttpClientFactory & AzureAdService Example
Problem with HttpClient
Solution → IHttpClientFactory
Registration
C# / .NET Questions 13
[Link]<AzureAdService>(); // Typed client
[Link]();
Lifetime Choice
Avoid Transient.
Q5. Middleware
Request pipeline
components: UseRouting , UseCors , UseAuthentication , UseAuthorization , UseSession .
Q7. CORS
Cross-Origin Resource Sharing → allows/blocks requests across domains.
C# / .NET Questions 14
Q11. Prevent multiple threads entering same code
Use: lock , Monitor , Mutex , SemaphoreSlim .
Action<T>: void.
int x = 5;
Update(ref x);
C# / .NET Questions 15
GetValues(out int a, out int b);
Works in-memory.
IQueryable
Q20. JWT
Client sends token → server validates signature & claims.
C# / .NET Questions 16
Q21. Rate Limiting
Restricts requests/time.
🔹 SQL Questions
Q1. Delete duplicates
WITH d AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY Col1, Col2 ORDER BY (S
ELECT 1)) rn
FROM MyTable
)
DELETE FROM d WHERE rn > 1;
SELECT MAX(Salary)
FROM Employee
WHERE Salary < (SELECT MAX(Salary) FROM Employee);
C# / .NET Questions 17
GROUP BY DeptId;
Q8. ACID
Atomicity: all or none.
Isolation: no interference.
Q9. Indexing
Purpose: faster lookups.
C# / .NET Questions 18
Q10. SQL Injection (with Example & Prevention)
Vulnerable
Attack
Stored Procedure
Entity Framework
C# / .NET Questions 19
Avoid SELECT * .
Forms
Example
Unnormalized:
Normalized:
Customers(CustomerId, Name)
Orders(OrderId, CustomerId, OrderDate)
OrderItems(OrderItemId, OrderId, ProductId)
🔹 LINQ Questions
1️⃣ What is LINQ? → Query collections in C#.
2️⃣ Types → Objects, Entities, SQL, XML.
3️⃣ IEnumerable vs IQueryable → client vs server.
4️⃣ Deferred Execution → runs when enumerated.
C# / .NET Questions 20
5️⃣ Immediate Execution → , . ToList() Count()
class BankAccount {
private decimal balance;
public void Deposit(decimal amt) => balance += amt;
public decimal GetBalance() => balance;
}
Inheritance
Polymorphism
Abstraction
C# / .NET Questions 21
🔹 SQL Analogies for OOP
Encapsulation → Views, Stored Procedures.
Orders
-----------------------------------------------------------------------
OrderId | CustomerName | CustomerAddress | Product1 | Product2
-----------------------------------------------------------------------
101 | Alice | 12 King St, Waterloo | Pen | Notebook
102 | Alice | 12 King St, Waterloo | Pencil | Notebook
103 | Bob | 88 Queen St, Kitchener | Pen | Ruler
Problems:
C# / .NET Questions 22
1NF (First Normal Form): make values atomic; no
repeating groups
One value per cell.
Orders
---------------------------------
OrderId | CustomerName | Address
---------------------------------
101 | Alice | 12 King St, Waterloo
102 | Alice | 12 King St, Waterloo
103 | Bob | 88 Queen St, Kitchener
OrderItems
-----------------------------
OrderId | Product
-----------------------------
101 | Pen
101 | Notebook
102 | Pencil
102 | Notebook
103 | Pen
103 | Ruler
C# / .NET Questions 23
Customers
-------------------------------
CustomerId | Name | Address
-------------------------------
1 | Alice | 12 King St, Waterloo
2 | Bob | 88 Queen St, Kitchener
Orders
-------------------------
OrderId | CustomerId | OrderDate
-------------------------
101 | 1 | 2025-09-03
102 |1 | 2025-09-03
103 |2 | 2025-09-03
Products
------------------
ProductId | Name
------------------
10 | Pen
11 | Notebook
12 | Pencil
13 | Ruler
OrderItems
-----------------------------
OrderId | ProductId | Qty
-----------------------------
101 | 10 |1
101 | 11 |1
102 | 12 |1
102 | 11 |1
103 | 10 |1
103 | 13 |1
C# / .NET Questions 24
3NF (Third Normal Form): remove transitive
dependencies
Non-key columns shouldn’t depend on other non-key columns.
Example: if Customers had PostalCode and we also stored City that can be
derived from PostalCode , that’s a transitive dependency. Store the lookup in
one place (e.g., PostalCodes table) or compute it.
Rule of thumb: Every non-key column should depend only on the key, the
whole key, and nothing but the key.
Why normalize?
Consistency: update a product name once, everywhere it’s used is correct.
you’re five 😊
2) The WITH query (CTE) — explain like
Imagine you’re building a LEGO house. First you make a small base and give it
a name (“base”). Then you say: “Now, using that base, build the whole house.”
A Common Table Expression (CTE) is like naming your small base so you
can use it right away to build something bigger.
Then you use that name in your main query—just like using your LEGO base
to build the house.
It helps you:
C# / .NET Questions 25
2a) Using WITH to find and delete
duplicates (classic interview favorite)
Goal: keep 1 row per “natural key” (e.g., Email ), delete the extras.
How it works:
WITH ranked AS (
SELECT Id, Email,
ROW_NUMBER() OVER (PARTITION BY Email ORDER BY Id) AS rn
FROM [Link]
)
SELECT * FROM ranked WHERE rn > 1; -- preview what you'd delete
Cleaner to read.
C# / .NET Questions 26
Works great with window functions like ROW_NUMBER() .
WITH spend AS (
SELECT [Link], SUM([Link] * [Link]) AS TotalSpend
FROM Orders o
JOIN OrderItems oi ON [Link] = [Link]
JOIN Products p ON [Link] = [Link]
GROUP BY [Link]
)
SELECT [Link], [Link]
FROM spend s
JOIN Customers c ON [Link] = [Link]
WHERE [Link] > 500
ORDER BY [Link] DESC;
WITH Org AS (
SELECT EmployeeId, ManagerId, Name, 0 AS Level
FROM Employees
WHERE ManagerId IS NULL -- top boss
UNION ALL
C# / .NET Questions 27
)
SELECT * FROM Org ORDER BY Level, Name;
DELETE u
FROM [Link] u
JOIN [Link] uKeep
ON [Link] = [Link]
AND [Link] > [Link]; -- delete higher Ids; keep the lowest
C# / .NET Questions 28
Wrap in a transaction if it’s production.
TL;DR
Normalization = split big, repetitive tables into clean related tables (1NF →
2NF → 3NF) so each fact is stored once.
WITH / CTE = name a mini-result and then use it in the main query—great
for readability, window functions, recursive queries.
C# / .NET Questions 29