1. 🔴 What is C#, and what are its main features?
C# (C-Sharp) is a modern, object-oriented, strongly typed programming language developed by Microsoft.
It is mainly used to develop Web Applications, Web APIs, Desktop Applications, Cloud Applications, and Enterprise Applications.
Main Features:
-
Object-Oriented: Supports Encapsulation, Inheritance, Polymorphism, and Abstraction.
-
Strongly Typed: Checks data types to prevent type-related errors.
-
Automatic Memory Management: Garbage Collector (GC) automatically manages managed memory.
-
Platform Independent: Modern .NET applications can run on Windows, Linux, and macOS.
-
Exception Handling: Uses
try,catch, andfinallyto handle errors. -
Async Programming: Supports
asyncandawaitfor asynchronous operations.
Simple Example:
Console.WriteLine("Hello World");
Interview Key Point: C# is a strongly typed, object-oriented programming language used to build applications on the .NET platform.
2. 🔴 What is the difference between C# and .NET?
C# is a programming language, whereas .NET is a development platform used to build and run applications.
|
C# |
.NET |
|---|---|
|
Programming language |
Development platform |
|
Used to write application code |
Used to build and execute applications |
|
Supports OOP concepts |
Provides CLR, libraries, and runtime services |
|
Example: Classes, Methods, Interfaces |
Example: CLR, ASP.NET Core, Base Class Library |
Simple Example:
-
C#: We write application logic using C#.
-
.NET: Provides the runtime and libraries required to execute the application.
Interview Key Point: C# is the language used to write code, while .NET provides the runtime, libraries, and tools to build and execute applications.
3. 🔴 What is CLR, and how does it execute C# code?
CLR (Common Language Runtime) is the execution engine of .NET. It is responsible for executing managed code.
How CLR Executes C# Code:
C# Source Code (.cs)
|
v
C# Compiler
|
v
IL Code (Intermediate Language)
|
v
CLR (.NET Runtime)
|
v
JIT Compiler
|
v
Native Machine Code
|
v
Execution
Main Responsibilities of CLR:
-
Memory Management: Allocates and manages managed memory.
-
Garbage Collection: Automatically releases memory occupied by unreachable managed objects.
-
Exception Handling: Supports handling runtime exceptions.
-
Type Safety: Helps prevent invalid type operations.
-
JIT Compilation: Converts IL code into native machine code during execution.
Interview Key Point: CLR is the .NET execution engine that manages code execution, memory, garbage collection, and exception handling. JIT converts IL into native machine code.
4. 🔴 What is the difference between managed and unmanaged code?
Managed Code: Code executed under the control of CLR is called managed code.
Unmanaged Code: Code executed outside the control of CLR is called unmanaged code.
|
Managed Code |
Unmanaged Code |
|---|---|
|
Executed under CLR |
Executed outside CLR |
|
Memory managed by Garbage Collector for managed objects |
Memory management is typically manual or handled by native libraries |
|
Provides runtime services such as type safety |
Does not receive CLR services automatically |
|
Example: C# application running on .NET |
Example: Native C/C++ application |
Managed Code Example:
class Employee
{
public string Name { get; set; }
}
Here, the CLR manages the lifetime of Employee objects through garbage collection.
Unmanaged Code Example:
int* ptr = new int(100);
delete ptr;
Here, memory is allocated and explicitly released using C++.
Interview Key Point: Managed code runs under CLR with automatic memory management, while unmanaged code runs outside CLR and typically requires explicit resource management.
Quick Interview Revision
|
Question |
Remember |
|---|---|
|
What is C#? |
Object-oriented, strongly typed programming language |
|
What is .NET? |
Development platform with runtime and libraries |
|
What is CLR? |
Execution engine of .NET |
|
What is IL? |
Intermediate Language generated by compiler |
|
What is JIT? |
Converts IL into native machine code |
|
What is managed code? |
Code executed under CLR |
|
What is unmanaged code? |
Code executed outside CLR |
|
What is GC? |
Automatically reclaims unused managed memory |
Important: Remember the execution flow: C# → Compiler → IL → CLR/JIT → Machine Code → Execution. This is a commonly asked .NET interview concept.