Complete Guide To C
Complete Guide To C
NET Core
1. C# Concepts
b. Important C# Features
Value Types & Reference Types: int, struct (value) vs class, string (reference).
Delegates & Events: Function pointers for callbacks (Action, Func, Predicate).
Collections in C#
Example:
numbers.Add(4); // [1, 2, 3, 4]
Example:
{1, "Alice"},
{2, "Bob"}
};
Console.WriteLine(users[1]); // "Alice"
✅ HashSet<T> (Unique Items)
Example:
Console.WriteLine(uniqueNumbers.Count); // Output: 3
Example:
tasks.Enqueue("Task 1");
tasks.Enqueue("Task 2");
c. Advanced C# Topics
Memory Management:
o Stack vs Heap Memory: Stack stores value types and method calls, Heap
stores reference types and dynamic memory allocation.
SOLID Principles:
Design Patterns:
o Singleton: Ensures a class has only one instance.
3. ASP.NET Framework
a. ADO.NET Components
SqlConnection: Establishes a connection to the database.
c. Repository Pattern – create a abstraction layer b/w the controller and the model
4. ASP.NET Core
Registering Middleware
app.UseMiddleware<CustomMiddleware>();
Web API:
JSON Uses:
When you push code using git push, it first goes to a remote Git server (e.g., GitHub,
Azure DevOps, or Bitbucket).
Until deployment happens, the code is only stored in the remote Git repository.
3. Build Step:
o Restore dependencies (dotnet restore).
5. Deploy to Azure:
Flow:
When you push code using git push, it first goes to a remote Git server (e.g., GitHub,
Azure DevOps, or Bitbucket).
Until deployment happens, the code is only stored in the remote Git repository.
It provides both Code First (starting from models) and Database First (starting from
DB) approaches.
It allows front-end applications (React, Angular, etc.) to interact with the back-end
through HTTP requests.
Since it deals with JSON input/output, it's widely used in web and mobile apps.
It shows method types (GET, POST, PUT, DELETE), URL paths, and parameters.
3. What is the purpose of WebApi?
Web API in .NET is used to create RESTful services that allow applications to communicate
over HTTP. It is mainly used for:
Ensures that resources (like files, database connections) are automatically disposed
of after use.
Example:
Console.WriteLine(sr.ReadToEnd());
object: Can store any data type, but requires explicit casting to access specific
properties or methods.
Example:
csharp
CopyEdit
Example:
csharp
CopyEdit
return 10;
Eliminates the need for manual SQL queries by mapping objects to database tables.
Code First: Define the model classes first, and EF creates the database automatically.
Database First: Start with an existing database, and EF generates model classes from
it.
10. Purpose of 'this' Keyword in C#
Used to differentiate instance variables from parameters when they have the same
name.
Example:
csharp
CopyEdit
class Person
Example:
javascript
CopyEdit
function test() {
let x = 10;
console.log(x);
test();
12 Difference Between ref and out Keywords
int a = 5;
int b;
✅ String (Immutable)
✅ StringBuilder (Mutable)
✅ Extension Methods add new methods to existing types without modifying their
source code.
// Usage
Console.WriteLine(text.WordCount()); // Output: 2
✅ Dependency Injection (DI) is a design pattern that reduces tight coupling between
classes by injecting dependencies instead of creating them inside the class.
2️. Implement the Interface – Create a class that implements the interface logic.
3️.Register the Service in DI Container – Add the dependency to the Services collection
in Program.cs using AddSingleton, AddScoped, or AddTransient.
4️. Inject Dependency in Constructor – Use constructor injection to pass the
dependency into the required class (e.g., controller, service, or repository).
5️. Use the Service – Call the injected service inside the class where it is needed.
This ensures loose coupling, better testability, and easier maintenance of the
application.
Lazy Loading means related data is only loaded when accessed, reducing initial query
load.
3️⃣ Use Virtual Navigation Properties – Mark related entities as virtual to allow lazy
loading.
4️⃣ Access Related Data – The related entity is loaded only when accessed for the first
time.
5️⃣ Be Aware of Performance Issues – Avoid N+1 query problems by using Include()
when necessary.
Middleware are components that handle HTTP requests and responses in the request
pipeline
1️⃣ Create a Middleware Component – Define a custom middleware class or use inline
middleware in Program.cs.
3️⃣ Call next() to Pass Control – Ensure the request moves to the next middleware in the
pipeline.
5️⃣ Ensure Proper Order – Place middleware components correctly (e.g., UseRouting
before UseAuthorization).
19. LINQ (Language Integrated Query) is a feature in C# used to query and manipulate
data from collections, databases, XML, and other sources using a concise and readable
syntax.
It helps perform operations like filtering, sorting, grouping, and aggregation in an easy
way.
1️⃣ ADO.NET
2️⃣ LINQ
Run:
csharp
CopyEdit
csharp
CopyEdit
options.UseSqlServer("your_connection_string_here");
Run:
sh
CopyEdit
✅ Insert Data
csharp
CopyEdit
context.Products.Add(product);
✅ Retrieve Data
csharp
CopyEdit
✅ Update Data
csharp
CopyEdit
if (product != null)
product.Price = 1000;
✅ Delete Data
csharp
CopyEdit
if (product != null)
context.Products.Remove(product);
Conclusion
Entity Framework removes the need to write SQL manually by allowing developers to
work with C# objects and LINQ instead of raw queries. 🚀
Basic Questions
A data structure is a way of organizing and storing data efficiently for easy access and
modification.
🔹 Examples: Array, Linked List, Stack, Queue, Hash Table, Tree, Graph
1️⃣ Array
Data and
A pointer to the next node.
Types:
Doubly Linked List → Each node points to the next & previous.
Operations:
Operations:
Types:
6️⃣ Tree
Types:
7️⃣ Graph
🔹 Sorting (Definition)
Sorting is the process of arranging elements in a specific order (ascending or descending) to
improve search efficiency and readability.
📌 Example: Sorting student names in alphabetical order.
🔹 Searching (Definition)
Searching is the process of finding a specific element in a dataset. Efficient searching
techniques help locate data quickly.
📌 Example: Finding a contact in your phonebook.
🔹 Sorting Algorithms
1️⃣ Bubble Sort
A simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong
order.
📌 Time Complexity: O(n²) (Slow for large data).
📌 Best Used For: Small datasets, Teaching sorting concepts.
🔹 Searching Algorithms
1️⃣ Linear Search
Checks each element one by one until the target is found.
📌 Time Complexity: O(n) (Slow for large data).
📌 Best Used For: Unsorted lists, Small datasets.
1️⃣ What are the differences between an array and a linked list?
Array: Fixed size, stored in contiguous memory, direct access (O(1) time complexity).
Linked List: Dynamic size, stored in non-contiguous memory, sequential access (O(n)
time complexity).
2️⃣ How does a stack work? What are its real-world applications?
Difference: Stack removes the last inserted item, while Queue removes the first
inserted item.
4️⃣ Explain the differences between a singly linked list and a doubly linked list.
Singly Linked List: Each node has a data field and a next pointer (points to the next
node).
Doubly Linked List: Each node has data, next, and an additional prev pointer (points
to the previous node).
Advantage of DLL: Allows traversal in both directions, better for complex operations.
Collision Handling Techniques: Chaining (linked lists at each index) and Open
Addressing (probing for next available slot).
Sorting & Searching
6️⃣ Explain different sorting algorithms (Bubble Sort, QuickSort, Merge Sort).
Bubble Sort: Repeatedly swaps adjacent elements if they are in the wrong order.
O(n²) time complexity.
QuickSort: Picks a pivot, partitions the array, and sorts recursively. O(n log n) on
average, O(n²) in the worst case.
Merge Sort: Divides array into halves, sorts recursively, and merges them. O(n log n)
time complexity, stable sorting.
7️⃣ What is binary search, and when should you use it?
Binary search works on a sorted array by dividing it into halves and searching
recursively.
2. Open Addressing: Finds the next available slot in case of collision (Linear
Probing, Quadratic Probing, etc.).
Worst case: O(n²) (when the pivot is always the smallest or largest element).
Average case: O(n log n) (when pivot divides the array into balanced halves).
🔟 What is the difference between breadth-first search (BFS) and depth-first search
(DFS)?
SQL
Drop table Employees;
FROM Customers;
Joins are used to combine rows from two or more tables based on a related column.
sql
CopyEdit
FROM Employees
Returns all records from the left table and matching records from the right table.
sql
CopyEdit
FROM Employees
✔ Returns all employees, even if they don’t have a department (NULL for missing data).
Returns all records from the right table and matching records from the left table.
sql
CopyEdit
FROM Employees
✔ Returns all departments, even if no employees belong to them (NULL for missing data).
Returns all records from both tables, filling missing matches with NULLs.
sql
CopyEdit
FROM Employees
sql
CopyEdit
FROM Employees
FROM Employees A
Aggregate functions perform calculations on multiple rows and return a single result. They
are often used with GROUP BY.
sql
CopyEdit
SELECT COUNT(DepartmentID) FROM Employees WHERE Age > 30; -- Counts employees
above 30
sql
CopyEdit
sql
CopyEdit
sql
CopyEdit
sql
CopyEdit
sql
CopyEdit
FROM Employees
GROUP BY DepartmentID;
sql
CopyEdit
SELECT * FROM Employees ORDER BY Age ASC; -- Sorts employees by age (smallest to
largest)
SELECT * FROM Employees ORDER BY Salary DESC; -- Sorts by Salary (highest to lowest)
Used to group rows based on a column, often used with aggregate functions (COUNT, SUM,
AVG, MAX, MIN).
sql
CopyEdit
FROM Employees
GROUP BY DepartmentID;
✔ Groups employees by DepartmentID and counts how many are in each department.
CopyEdit
FROM Employees
GROUP BY DepartmentID
sql
CopyEdit
SELECT * FROM Employees WHERE Name LIKE 'J%'; -- Names starting with 'J'
SELECT * FROM Employees WHERE Name LIKE '%son'; -- Names ending with 'son'
SELECT * FROM Employees WHERE Name LIKE '%an%'; -- Names containing 'an'
sql
CopyEdit
sql
CopyEdit
SELECT * FROM Employees WHERE Age BETWEEN 25 AND 40;
sql
CopyEdit
sql
CopyEdit
SELECT * FROM Employees ORDER BY Salary DESC LIMIT 3; -- Gets top 3 highest salaries