Site icon NOTE BAHADUR

UNIT I: Introducing C# and the .NET Framework

Object Orientation

Object Orientation (OO) is a programming paradigm that organizes software into objects that contain both data (properties) and behavior (methods). It helps developers write reusable, secure, and maintainable programs.

Object-oriented programming (OOP) is the foundation of C#. Instead of writing long procedural code, programs are divided into objects that interact with each other. C# supports the four pillars of OOP:

OOP Pillars

Encapsulation

Example

class Student

{

    public string Name;

    public void Display()

    {

        Console.WriteLine(Name);

    }

}

Real-World Scenario (Nepal)

A Khalti wallet stores a user’s balance privately. Users can access or modify it only through authorized functions like Load Money or Pay, not by directly changing the balance.

Diagram

Inheritance

Allows one class to acquire the properties and methods of another class.

Example

Vehicle

   |

 Car

A Car automatically gets common features from Vehicle.

Real-World Scenario (Nepal)

A Government Employee class can be inherited by Teacher, Police Officer, and Doctor, since all are government employees but have additional responsibilities.

Polymorphism

Allows one method to perform different tasks depending on the object.

Example

Draw()

Circle → Draw Circle

Rectangle → Draw Rectangle

Triangle → Draw Triangle

Abstraction

Shows only essential information while hiding implementation details.

Example

While using an ATM, users press buttons to withdraw money without knowing how the banking system processes the transaction.

Real-World Scenario (Nepal)

Using the eSewa mobile app. Users simply tap Pay Electricity Bill, while the application hides all backend processing.

Diagram

Advantages of OOP

Key Points for Exam

Type Safety

Type Safety is the feature of C# that prevents a variable from storing a value of an incompatible data type, reducing programming errors and improving program reliability.

C# is a strongly typed language. Every variable must have a declared data type, and the compiler checks type compatibility before execution.

Example

int age = 20;

string name = “Ram”;

Correct because the assigned values match the declared data types.

Invalid example:

int age = “Twenty”;

This produces a compile-time error because a string cannot be assigned to an integer.

Benefits of Type Safety

Compile-Time Type Checking

Real-World Scenario (Nepal)

In the National Identity Card Registration System, the Age field accepts only numeric values. If someone enters “Twenty” instead of 20, the system rejects the input before saving the record. This is similar to type safety in C#.

Key Points for Exam

Memory Management

Memory Management is the process of allocating and releasing memory automatically so that programs use system resources efficiently.

Unlike languages such as C++, C# automatically manages memory through the Garbage Collector (GC). Programmers generally do not need to free memory manually.

Types of Memory

Stack Memory

Heap Memory

Diagram

             

Garbage Collection (GC)

The Garbage Collector (GC) is a component of the CLR that automatically removes objects that are no longer used by the program, freeing heap memory.

Working Process

Example

Student s = new Student();

When s is no longer referenced, the Garbage Collector eventually removes the object from memory.

Advantages of Automatic Memory Management

Real-World Scenario (Nepal)

Consider a TU Online Admission Portal. Thousands of student records are processed every day. Once a request is completed, the temporary objects used for processing are automatically removed from memory by the Garbage Collector, allowing the server to continue handling new requests efficiently.

Difference Between Stack and Heap

StackHeap
Stores local variablesStores objects
FasterComparatively slower
Automatically released after method executionReleased by Garbage Collector
Small memoryLarge memory
LIFO structureDynamic memory allocation

Key Points for Exam

Platform Support

Platform Support is the capability of C# and the .NET Framework to develop and execute applications on different operating systems and hardware platforms with minimal code changes.

Initially, the .NET Framework was designed for Windows. With the introduction of .NET Core and later .NET (5/6/7/8), C# applications became cross-platform and can run on Windows, Linux, and macOS.

Types of Platform Support

Example

A developer creates an ASP.NET Core web application on Windows. The same application can be hosted on a Linux server without changing its source code.

Real-World Scenario (Nepal)

A software company in Kathmandu develops an online college management system using ASP.NET Core on Windows laptops. The system is later deployed to a Linux cloud server, allowing students and teachers across Nepal to access it through a web browser.

Diagram

Advantages of Platform Support

Key Points for Exam

C# and CLR

Common Language Runtime (CLR) is the execution engine of the .NET Framework that manages the execution of C# programs by providing services such as memory management, security, exception handling, and garbage collection.

When a C# program is compiled, it is not converted directly into machine code. Instead, it is converted into Intermediate Language (IL). The CLR then uses the Just-In-Time (JIT) Compiler to convert IL into machine code during execution.

Program Execution Process

Responsibilities of CLR

Memory Management

Garbage Collection

Exception Handling

Security

Thread Management

Language Interoperability

Example

using System;

class Program

{

    static void Main()

    {

        Console.WriteLine(“Hello, .NET”);

    }

}

Execution Steps:

  1. C# compiler converts the code into IL.
  2. CLR loads the IL.
  3. JIT Compiler converts IL into machine code.
  4. The program executes.

Real-World Scenario (Nepal)

A banking application developed in C# by a financial institution in Nepal relies on the CLR to manage memory, protect sensitive customer data, and recover gracefully from runtime exceptions, ensuring reliable transactions.

Diagram

Advantages of CLR

Key Points for Exam

CLR and .NET Framework

.NET Framework is a software development platform provided by Microsoft, while the Common Language Runtime (CLR) is its execution engine responsible for running .NET applications.

The .NET Framework provides libraries and APIs for application development, whereas the CLR executes the compiled code and manages runtime services.

Components of the .NET Framework

Common Language Runtime (CLR)

Framework Class Library (FCL)

Languages Supported

Architecture of .NET Framework

CLR vs .NET Framework

CLR.NET Framework
Runtime environmentSoftware development platform
Executes programsProvides libraries and runtime
Performs JIT compilationContains CLR and FCL
Handles memory managementUsed to build applications
Performs Garbage CollectionSupports multiple programming languages

Framework Class Library (FCL)

The Framework Class Library (FCL) is a collection of predefined classes and namespaces that simplify software development.

Common Namespaces

NamespacePurpose
SystemBasic classes
System.IOFile handling
System.CollectionsCollections
System.NetNetworking
System.DataDatabase programming
System.WebWeb applications

Example

using System;

class Program

{

    static void Main()

    {

        DateTime today = DateTime.Now;

        Console.WriteLine(today);

    }

}

Here, the System namespace and DateTime class are provided by the Framework Class Library.

Real-World Scenario (Nepal)

A university develops an online examination system using the .NET Framework. The application uses:

Diagram

Advantages of the .NET Framework

Key Points for Exam

Other Frameworks

Other Frameworks are Microsoft and third-party .NET implementations that allow developers to build applications for different platforms such as desktop, web, cloud, and mobile devices.

Besides the traditional .NET Framework, Microsoft introduced several frameworks to overcome platform limitations and improve performance.

.NET Core

.NET Core is an open-source, cross-platform framework developed by Microsoft for building modern applications.

Unlike the traditional .NET Framework, .NET Core supports Windows, Linux, and macOS.

Features

Example

A developer creates an ASP.NET Core website on Windows and deploys it to a Linux server without modifying the source code.

Real-World Scenario (Nepal)

A startup in Kathmandu develops an online food delivery system using ASP.NET Core and hosts it on a Linux cloud server, reducing hosting costs while maintaining performance.

Mono

Mono is an open-source implementation of the .NET Framework that enables .NET applications to run on non-Windows platforms.

Mono was widely used before the release of .NET Core and is still used in some legacy applications.

Features

Example

Unity game developers often use Mono to execute C# scripts during development.

Real-World Scenario (Nepal)

A game development company in Nepal uses Unity with Mono to build educational games for schools.

Xamarin

Xamarin is a Microsoft framework used to develop Android and iOS applications using C#.

Developers write one codebase that can be shared across multiple mobile platforms.

Features

Example

One C# application can run on both Android and iPhone.

Real-World Scenario (Nepal)

A hospital develops a mobile appointment system using Xamarin, allowing both Android and iPhone users to book appointments.

.NET MAUI

.NET Multi-platform App UI (MAUI) is the successor to Xamarin used for developing desktop and mobile applications from a single codebase.

Supported Platforms

Diagram

Key Points for Exam

Framework Overview

Framework Overview refers to the overall architecture and components of the .NET Framework that work together to develop and execute applications efficiently.

The .NET Framework provides a complete environment for software development by combining runtime services with reusable libraries.

Main Components

1. Common Language Runtime (CLR)

Responsible for:

2. Framework Class Library (FCL)

Provides reusable classes for:

3. Languages

Applications can be written using:

4. Applications

Using .NET, developers can build:

Overall Architecture

Real-World Scenario (Nepal)

A university management system uses:

All these components work together to manage admissions, examinations, and result processing.

Key Points for Exam

.NET Standard 2.0

.NET Standard 2.0 is a specification that defines a common set of APIs that all .NET implementations must support, ensuring code compatibility across different .NET platforms.

It is not a runtime or framework. Instead, it acts as a contract that allows developers to create reusable libraries.

Purpose

Supported Platforms

Diagram

Example

A class library developed using .NET Standard can be used by:

without changing the source code.

Real-World Scenario (Nepal)

A software company develops a Student Library Management Library using .NET Standard. The same library is reused in desktop software, mobile applications, and web portals for multiple colleges.

Advantages

Key Points for Exam

Applied Technologies

Applied Technologies are real-world technologies and application areas where C# and the .NET platform are used to develop software solutions.

Desktop Applications

Developed using:

Examples

Web Applications

Developed using:

Examples

Mobile Applications

Developed using:

Examples

Cloud Applications

Developed using:

Examples include:

Database Applications

Developed using:

Examples:

Game Development

Developed using:

Examples:

Internet of Things (IoT)

Developed using:

Examples:

Diagram

Real-World Scenario (Nepal)

Many organizations in Nepal use .NET technologies:

OrganizationPossible Application
BanksInternet Banking System
HospitalsPatient Management
CollegesStudent Management
Government OfficesCitizen Information Systems
E-commerce CompaniesOnline Shopping Platforms

Advantages of .NET Technologies

Key Points for Exam

Important Terms to Remember

Frequently Asked TU Examination Questions

Short Questions (5 Marks)

Long Questions (10 Marks)

Exit mobile version