NET Centric Computing · Unit 1
Core .NET Framework Fundamentals
Exam-focused notes for Core .NET Framework Fundamentals (NET Centric Computing, BIT351): what the TU syllabus asks and how it has actually been tested, with 7 solved past questions from this unit.
Core .NET Framework Fundamentals in short
The .NET Framework is the base on which C# programs are built and run. This chapter explains how code is compiled and run, and the core C# features used in every ASP.NET Core project.
What this unit covers
- MSIL and CLR architecture
- Lambda expressions
- Abstract classes and inheritance
- Polymorphism and code extensibility
- Base keyword usage
- Indexers
- Collections and generics
- Partial classes
Indexers
2081 · 10 marks
Define indexers. What is the use of base keyword? How can you apply polymorphism in code extensibility?[10]
Show solved answerHide answer
Indexers, Base Keyword, and Polymorphism in Code Extensibility
Note: No specific curriculum notes were provided for this question. The answer below is based on standard C# / Object-Oriented Programming concepts as taught in TU BSc CSIT curriculum.
1. Indexers (3 marks)
Definition
An indexer is a special class member in C# that allows an object to be indexed like an array. It enables instances of a class to be accessed using the subscript notation ([]), making the class behave like a virtual array.
Syntax
<access_modifier> <return_type> this[<parameter_type> index]
{
get { // return value }
set { // set value }
}
Example
class StudentMarks
{
private int[] marks = new int[5];
// Indexer definition
public int this[int index]
{
get
{
return marks[index];
}
set
{
marks[index] = value;
}
}
}
class Program
{
static void Main()
{
StudentMarks s = new StudentMarks();
s[0] = 85; // uses set accessor
s[1] = 90; // uses set accessor
Console.WriteLine(s[0]); // uses get accessor -> Output: 85
}
}
Key Features of Indexers
| Feature | Description |
|---|---|
| Keyword used | this |
| Can be overloaded | Yes |
| Can have get/set | Yes |
| Parameter type | Can be int, string, or any type |
| Access modifier | Can be public, private, etc. |
2. The base Keyword (3 marks)
Definition
The base keyword in C# is used to access members of the base (parent) class from within a derived (child) class. It is used when a derived class wants to call or refer to the parent class's constructor, methods, or properties that have been overridden.
Uses of base Keyword
a) Calling Base Class Constructor
class Animal
{
string name;
public Animal(string name)
{
this.name = name;
Console.WriteLine("Animal constructor called: " + name);
}
}
class Dog : Animal
{
public Dog(string name) : base(name) // calls Animal's constructor
{
Console.WriteLine("Dog constructor called");
}
}
b) Calling Overridden Base Class Method
class Shape
{
public virtual void Draw()
{
Console.WriteLine("Drawing a Shape");
}
}
class Circle : Shape
{
public override void Draw()
{
base.Draw(); // calls Shape's Draw() method
Console.WriteLine("Drawing a Circle");
}
}
Output:
Drawing a Shape
Drawing a Circle
Summary of base Keyword Uses
base.MethodName()-- calls the parent class methodbase(arguments)-- calls the parent class constructor- Prevents ambiguity when method names are the same in parent and child classes
3. Polymorphism in Code Extensibility (4 marks)
Definition
Polymorphism means "many forms." It is the ability of a method, object, or variable to take multiple forms. In OOP, polymorphism allows a single interface to represent different underlying data types or behaviors.
Types of Polymorphism
| Type | Also Known As | Achieved By |
|---|---|---|
| Compile-time | Static / Early Binding | Method Overloading, Operator Overloading |
| Run-time | Dynamic / Late Binding | Method Overriding (virtual/override) |
How Polymorphism Enables Code Extensibility
Code extensibility means the ability to add new functionality without modifying existing code. Polymorphism supports this through:
- Open/Closed Principle -- classes are open for extension but closed for modification.
- Runtime polymorphism allows new derived classes to be added without changing the base class or calling code.
Example: Polymorphism for Code Extensibility
// Base class
class Shape
{
public virtual double Area()
{
return 0;
}
public void Display()
{
Console.WriteLine("Area = " + Area());
}
}
// Derived class 1
class Circle : Shape
{
double radius;
public Circle(double r) { radius = r; }
public override double Area()
{
return 3.14 * radius * radius;
}
}
// Derived class 2
class Rectangle : Shape
{
double length, width;
public Rectangle(double l, double w) { length = l; width = w; }
public override double Area()
{
return length * width;
}
}
// NEW class added WITHOUT modifying existing code
class Triangle : Shape
{
double base_t, height;
public Triangle(double b, double h) { base_t = b; height = h; }
public override double Area()
{
return 0.5 * base_t * height;
}
}
class Program
{
static void Main()
{
// Polymorphic array
Shape[] shapes = new Shape[3];
shapes[0] = new Circle(5);
shapes[1] = new Rectangle(4, 6);
shapes[2] = new Triangle(3, 8);
foreach (Shape s in shapes)
{
s.Display(); // calls the correct overridden Area()
}
}
}
Output:
Area = 78.5
Area = 24
Area = 12
Explanation of Extensibility
- The
Display()method inShapecallsArea()which is virtual. - When a new shape like
Triangleis added
Collections and generics
2080 · 5 marks
Differentiate between collection and generics. [5]
MSIL and CLR architecture
2080 · 5 marks
Explain about MSIL and CLR. [5]
Show solved answerHide answer
MSIL and CLR
Note: Reference notes were not available for this topic. The following answer is based on standard .NET / C# curriculum content taught in BSc CSIT.
1. MSIL (Microsoft Intermediate Language)
MSIL (also called CIL - Common Intermediate Language) is a low-level, platform-independent instruction set into which all .NET-compatible languages (C#, VB.NET, F#, etc.) are compiled before being converted to native machine code.
Key Points about MSIL:
- When a programmer writes code in C# (or any .NET language) and compiles it, the compiler does not produce native machine code directly.
- Instead, it produces MSIL code stored inside an assembly (
.exeor.dllfile). - MSIL is CPU-independent - it is not tied to any specific processor architecture.
- MSIL contains instructions for loading, storing, initializing, and calling methods on objects.
- It also contains metadata that describes the types, members, and references in the code.
Compilation Flow:
Source Code (.cs)
|
v
C# Compiler
|
v
MSIL + Metadata (stored in Assembly .exe/.dll)
|
v
CLR (JIT Compiler)
|
v
Native Machine Code (executed by CPU)
2. CLR (Common Language Runtime)
CLR is the execution engine (runtime environment) of the .NET Framework. It is responsible for loading, executing, and managing .NET programs.
Key Responsibilities of CLR:
| Feature | Description |
|---|---|
| JIT Compilation | Converts MSIL into native machine code at runtime using Just-In-Time compiler |
| Memory Management | Automatically allocates and deallocates memory |
| Garbage Collection | Automatically reclaims unused objects from heap memory |
| Type Safety | Ensures code accesses only the memory it is authorized to access |
| Exception Handling | Provides a structured mechanism to handle runtime errors |
| Security | Enforces code access security and role-based security |
| Thread Management | Manages multithreading and synchronization |
| CTS & CLS Support | Enforces Common Type System and Common Language Specification for language interoperability |
How CLR Works:
- The CLR loads the assembly (containing MSIL).
- The JIT compiler translates MSIL into native machine code specific to the current CPU.
- The native code is executed by the operating system.
- CLR continuously manages the running application (GC, exceptions, security, etc.).
Relationship Between MSIL and CLR
- MSIL is the intermediate form of code; CLR is the engine that runs it.
- Without CLR, MSIL cannot be executed.
- This design allows language interoperability - code written in different .NET languages can work together because they all compile to the same MSIL, and CLR executes all of them uniformly.
Summary
MSIL is the intermediate, platform-independent bytecode produced by .NET compilers, while CLR is the runtime environment that JIT-compiles MSIL into native code and manages program execution, memory, security, and other services.
2081.1 · 5 marks
Explain about MSIL and CLR. [5]
Show solved answerHide answer
MSIL and CLR
Note: The reference notes did not contain this topic. The following answer is based on standard .NET / C# curriculum content taught in BSc CSIT.
1. MSIL (Microsoft Intermediate Language)
MSIL (also called CIL - Common Intermediate Language) is a low-level, platform-independent instruction set into which all .NET-compatible languages (C#, VB.NET, F#, etc.) are compiled before being converted to native machine code.
Key Points:
- When a programmer writes code in C# (or any .NET language) and compiles it, the compiler does not produce native machine code directly.
- Instead, it produces MSIL code stored inside an assembly (
.exeor.dllfile). - MSIL is CPU-independent - it is not tied to any specific processor or operating system.
- MSIL contains instructions for loading, storing, calling methods, arithmetic operations, etc., in a generalized form.
- It also contains metadata - information about types, methods, and references used in the program.
Compilation Flow:
Source Code (.cs)
|
[C# Compiler]
|
MSIL Code + Metadata (stored in Assembly)
|
[CLR / JIT Compiler]
|
Native Machine Code (executed by CPU)
2. CLR (Common Language Runtime)
CLR is the execution engine (runtime environment) of the .NET Framework. It is responsible for loading, executing, and managing .NET programs.
Key Responsibilities of CLR:
| Function | Description |
|---|---|
| JIT Compilation | Converts MSIL into native machine code at runtime using the Just-In-Time (JIT) compiler |
| Memory Management | Automatically allocates and deallocates memory |
| Garbage Collection (GC) | Automatically reclaims unused objects from the heap |
| Type Safety | Ensures that code accesses only the memory it is authorized to access |
| Exception Handling | Provides a structured mechanism to handle runtime errors |
| Security | Enforces code access security and role-based security |
| Thread Management | Manages multithreading and synchronization |
| Interoperability | Allows .NET code to interact with unmanaged (native) code |
How CLR Works:
- The CLR loads the assembly (containing MSIL).
- The JIT compiler inside CLR converts MSIL to native machine code specific to the current CPU.
- The native code is then executed by the operating system.
- CLR continuously manages the running program (GC, exceptions, security, etc.).
Relationship Between MSIL and CLR
| MSIL | CLR |
|---|---|
| Intermediate code produced by compiler | Runtime that executes the intermediate code |
| Platform-independent | Platform-specific (different CLR for different OS) |
| Stored in assembly files | Installed as part of .NET Framework |
| Input to CLR | Processes and runs MSIL |
Summary
MSIL is the intermediate, platform-independent bytecode that .NET compilers produce. CLR is the runtime environment that takes this MSIL, compiles it to native code via JIT, and manages its execution including memory, security, and exception handling. Together, they form the foundation of the .NET execution model.
Abstract classes and inheritance
2080 · 5 marks
Why do we need abstarct class? Explain with an example. [5]
Base keyword usage
2081.1 · 5 marks
Define JSON. What is the use of base keyword? Give an example. [5]
Lambda expressions
0 · 5 marks
What is lambda expression? Describe the needs of asynchronous programming. [5]
Make Unit 1 stick
Practice BIT351 with flashcards & quizzes