1 Core Net Framework Fundamentals

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.

Core .NET Framework Fundamentals: notes

MSIL and CLR architecture

MSIL (Microsoft Intermediate Language, also called IL or CIL) is a CPU independent set of instructions. When you build a C# program, the compiler does not produce machine code. It produces MSIL plus metadata, which describes the types and members in the program. These are stored in an assembly (a .dll or .exe file).

The CLR (Common Language Runtime) is the engine that runs this code. It is the execution part of .NET.

How a program runs:

  • The C# compiler turns source code into MSIL and metadata.
  • The CLR loads the assembly and checks that the code is type safe.
  • The JIT (just-in-time) compiler converts MSIL into native machine code when a method is first called.
  • The native code runs, and the result is reused for later calls.

Main services of the CLR:

  • Memory management and garbage collection, which frees objects no longer in use.
  • Type safety checks and security.
  • Exception handling across languages.
  • Thread management.
  • Language interoperability, since C#, VB.NET and F# all compile to MSIL.

Because MSIL is not tied to a CPU, the same assembly can run on Windows, Linux or macOS when a suitable runtime is present.

Lambda expressions

A lambda expression is a short, unnamed function. It uses the operator => (read as "goes to"). The left side holds the parameters and the right side holds the expression or block.

Func<int, int> square = x => x * x;
Console.WriteLine(square(5));            // 25

var marks = new List<int> { 35, 80, 42, 90 };
var passed = marks.Where(m => m >= 40).ToList();   // 80, 42, 90

Lambdas are used with LINQ, delegates, events and callbacks. They keep the code short because no separate method is needed.

Asynchronous programming

In asynchronous programming, a long task such as a file read, database call or web request does not block the running thread. The thread is free to do other work while it waits. C# uses the async and await keywords with the Task type.

public async Task<string> GetPageAsync(string url)
{
    using var client = new HttpClient();
    string html = await client.GetStringAsync(url);   // thread is not blocked here
    return html;
}

Why it is needed:

  • A desktop or mobile app stays responsive because the UI thread is not blocked.
  • A web server can handle more requests with the same number of threads.
  • The code reads almost like normal top to bottom code.

An async lambda is also allowed: async () => await Task.Delay(100).

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 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

FeatureDescription
Keyword usedthis
Can be overloadedYes
Can have get/setYes
Parameter typeCan be int, string, or any type
Access modifierCan 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 method
  • base(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

TypeAlso Known AsAchieved By
Compile-timeStatic / Early BindingMethod Overloading, Operator Overloading
Run-timeDynamic / Late BindingMethod 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:

  1. Open/Closed Principle -- classes are open for extension but closed for modification.
  2. 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 in Shape calls Area() which is virtual.
  • When a new shape like Triangle is added
Open this answer on its own page →

Collections and generics

2080 · 5 marks

Differentiate between collection and generics. [5]

Note: The reference notes did not contain this topic directly. The following answer is based on standard Java/C programming concepts as taught in BSc CSIT curriculum. --- A Collection is a framework/architecture that provides a set of classes and interfaces to store, retrieve, manipulate, and com...

Open this answer on its own page →

MSIL and CLR architecture

2080 · 5 marks

Explain about MSIL and CLR. [5]

Show solved 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 (.exe or .dll file).
  • 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:

FeatureDescription
JIT CompilationConverts MSIL into native machine code at runtime using Just-In-Time compiler
Memory ManagementAutomatically allocates and deallocates memory
Garbage CollectionAutomatically reclaims unused objects from heap memory
Type SafetyEnsures code accesses only the memory it is authorized to access
Exception HandlingProvides a structured mechanism to handle runtime errors
SecurityEnforces code access security and role-based security
Thread ManagementManages multithreading and synchronization
CTS & CLS SupportEnforces Common Type System and Common Language Specification for language interoperability

How CLR Works:

  1. The CLR loads the assembly (containing MSIL).
  2. The JIT compiler translates MSIL into native machine code specific to the current CPU.
  3. The native code is executed by the operating system.
  4. 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.

Open this answer on its own page →

2081.1 · 5 marks

Explain about MSIL and CLR. [5]

Show solved 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 (.exe or .dll file).
  • 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:

FunctionDescription
JIT CompilationConverts MSIL into native machine code at runtime using the Just-In-Time (JIT) compiler
Memory ManagementAutomatically allocates and deallocates memory
Garbage Collection (GC)Automatically reclaims unused objects from the heap
Type SafetyEnsures that code accesses only the memory it is authorized to access
Exception HandlingProvides a structured mechanism to handle runtime errors
SecurityEnforces code access security and role-based security
Thread ManagementManages multithreading and synchronization
InteroperabilityAllows .NET code to interact with unmanaged (native) code

How CLR Works:

  1. The CLR loads the assembly (containing MSIL).
  2. The JIT compiler inside CLR converts MSIL to native machine code specific to the current CPU.
  3. The native code is then executed by the operating system.
  4. CLR continuously manages the running program (GC, exceptions, security, etc.).

Relationship Between MSIL and CLR

MSILCLR
Intermediate code produced by compilerRuntime that executes the intermediate code
Platform-independentPlatform-specific (different CLR for different OS)
Stored in assembly filesInstalled as part of .NET Framework
Input to CLRProcesses 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.

Open this answer on its own page →

Abstract classes and inheritance

2080 · 5 marks

Why do we need abstarct class? Explain with an example. [5]

An abstract class is a class that is deliberately incomplete. It is declared with the abstract keyword, it cannot be instantiated with new, and it may declare abstract members that carry no body at all. Its purpose is to sit at the top of a family of related classes and state two things at once: ...

Open this answer on its own page →

Base keyword usage

2081.1 · 5 marks

Define JSON. What is the use of base keyword? Give an example. [5]

--- JSON is a lightweight, text-based, language-independent data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is based on a subset of JavaScript but is language-independent and is widely used for transmitting data between a server a...

Open this answer on its own page →

Lambda expressions

0 · 5 marks

What is lambda expression? Describe the needs of asynchronous programming. [5]

A lambda expression is an anonymous function written inline, at the point where it is needed, instead of being declared as a named method somewhere else. In C it is written with the = token, called the lambda operator, with the parameters on the left and either a single expression or a statement ...

Open this answer on its own page →