.NET Framework lets developers write applications in multiple languages like C# and VB.NET that run on a common runtime. .NET Framework ensures that programs are executed safely, efficiently and in a platform-independent way (within Windows). Its main advantage is providing reusable code and consistent behavior across applications.
Table of Content
Characteristics of .NET Framework
- Supports multiple programming languages.
- Provides a huge class library for common operations.
- Handles memory management automatically through Garbage Collector.
- Ensures security through Code Access Security and type safety.
- Offers multiple application models: desktop, web and services.
To know more about Characteristics, refer to Characteristics of .NET Framework
Installing and Setting Up .NET
Before using the .NET Framework, developers need to set up the required SDK and tools.
- On Windows: .NET Framework is usually pre-installed with Windows OS. For development, install Visual Studio which comes bundled with .NET.
- On macOS/Linux: .NET Framework itself is Windows-only, but you can install .NET SDK (.NET Core / 5+) to develop cross-platform apps.
Refer to the below articles to get detailed information about Installation and Setting up .NET
- Install and Set Up .NET SDK on Windows, macOS and Linux
- How to Install .NET on Linux?
- How to Install and Setup Visual Studio for ASP.NET?
Architecture
The framework is designed in layers, each handling specific responsibilities.
1. Common Language Runtime (CLR):
- CLR is the execution engine of .NET applications.
- Provides memory management, garbage collection, exception handling and thread management.
- Ensures that code executes in a controlled environment.
2. Framework Class Library (FCL):
- FCL is a huge collection of reusable classes, interfaces and value types.
- Contains APIs for collections, file handling, database interaction, XML processing and more.
3. Languages and Compilers:
- Source code written in C#, VB.NET or other supported languages compiles into MSIL (Microsoft Intermediate Language).
- MSIL is then converted into native machine code by the JIT (Just-In-Time) compiler.
To know about Architecture of .NET Framework, refer to .NET Framework Architecture and Component Stack
Key Concepts
- MSIL (Microsoft Intermediate Language): MISL is the code generated by compilers that is platform-independent until runtime.
- JIT Compiler: JIT Compiler converts MSIL into machine-specific code when the program runs.
- CTS (Common Type System): Defines rules for how data types are declared and used across languages, ensuring compatibility.
- CLS (Common Language Specification): A subset of CTS that defines the rules all .NET languages must follow to remain interoperable.
- Managed vs Unmanaged Code: Managed and Unmanaged Code in .NET Framework refers to code executed under CLR vs code running directly on OS.
Application Models in .NET Framework
The framework supports multiple programming models, which makes it versatile:
1. Windows Forms (WinForms)
- WinForms are used for creating traditional desktop applications.
- Provides controls like buttons, labels and menus for building user interfaces.
using System;
using System.Windows.Forms;
class Program : Form {
static void Main() {
Application.Run(new Program());
}
public Program() {
Button btn = new Button { Text = "Click Me", Left = 50, Top = 50 };
btn.Click += (sender, e) => MessageBox.Show("Hello from WinForms!");
Controls.Add(btn);
}
}
2. ASP.NET
- ASP.NET is used for building web applications and services.
- Offers server controls, authentication, caching and session management.
3. ADO.NET
- ADO.NET handles database operations.
- Provides objects like SqlConnection, SqlCommand and DataSet to interact with databases.
using System.Data.SqlClient;
class Demo {
static void Main() {
using (SqlConnection con = new SqlConnection("connection_string")) {
con.Open();
SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM Employees", con);
int count = (int)cmd.ExecuteScalar();
Console.WriteLine($"Total Employees: {count}");
}
}
}
4. WPF (Windows Presentation Foundation)
- WPF is Advanced UI toolkit for desktop applications.
- Supports animations, data binding and rich graphics.
5. WCF (Windows Communication Foundation)
- Allows applications to communicate over networks.
- Used for creating service-oriented applications.
6. WF (Workflow Foundation)
- Automates business workflows using predefined activities.
Assemblies in .NET Framework
An assembly is the smallest deployable and reusable unit of a .NET application. It acts as the building block that contains:
- Compiled Code (MSIL): The actual executable instructions generated by the compiler.
- Metadata: Information about types, members, versioning and references.
- Manifest: Describes the assembly’s identity, referenced assemblies and security details.
- Resources: Files such as images, strings or configuration data.
Types of Assemblies
1. Private Assembly
- Designed for a single application.
- Stored in the application’s local directory.
- Ideal when only one project uses the code.
2. Shared Assembly
- Can be reused by multiple applications.
- Stored in the Global Assembly Cache (GAC).
- Useful for enterprise-level projects where common functionality is needed across apps.
Example:
When you compile a C# program, the compiler generates an .exe or .dll file. Both of these are assemblies. A console app usually produces an .exe, while class libraries produce .dll.
Namespaces in .NET Framework
A namespace is a logical grouping of related classes, interfaces and functions. They help in:
- Organizing code into hierarchical structures.
- Avoiding naming conflicts between classes.
- Improving readability and maintainability of large applications.
Commonly Used Namespaces
- System: Contains fundamental classes like String, Int32, Console.
- System.IO: Provides types for file and stream operations (File, StreamReader).
- System.Data: Offers classes for database interaction (DataSet, SqlConnection).
- System.Net: Used for networking and web-based operations (HttpWebRequest).
- System.Xml: Enables XML processing and parsing.
Example:
using System;
using System.IO;
class Demo {
static void Main() {
string path = "sample.txt";
File.WriteAllText(path, "Hello .NET Namespace Example");
Console.WriteLine(File.ReadAllText(path));
}
}
Here,
- System provides the Console class.
- System.IO provides the File class.
Namespaces ensure that even if two classes have the same name, they won’t conflict as long as they belong to different namespaces.
Exception Handling
The .NET Framework relies on C# exception handling to manage runtime errors. This mechanism ensures that applications do not crash abruptly and instead handle errors in a controlled way.
Key Components:
- try block: Holds the code that may generate an exception.
- catch block: Handles specific or general exceptions and provides user-friendly error messages.
- finally block: Contains code that must execute whether or not an exception occurs, such as resource cleanup.
- throw keyword: Used to manually raise exceptions.
using System;
class Demo {
static void Main() {
try {
int x = 10, y = 0;
int result = x / y;
}
catch (DivideByZeroException ex) {
Console.WriteLine("Error: Cannot divide by zero.");
}
catch (Exception ex) {
Console.WriteLine($"General Error: {ex.Message}");
}
finally {
Console.WriteLine("Cleanup code runs here.");
}
}
}
Explanation:
- If division by zero occurs, the DivideByZeroException block executes.
- Any other error is caught by the general Exception block.
- The finally block ensures cleanup logic (like closing a file or database connection) always runs.
Security in .NET Framework
The .NET Framework provides a comprehensive security model to ensure safe execution of applications. It combines code access control, type safety and role-based permissions to protect systems and users.
Main Security Features
1. Code Access Security (CAS):
- CAS Restricts what code can do depending on its source (local machine, intranet or internet).
- Example: A program downloaded from the internet may not have permission to access the local file system.
2. Validation and Verification:
- Before execution, the CLR verifies MSIL code for type safety and ensures it follows rules of the Common Type System (CTS).
- Prevents unsafe operations like accessing memory directly or executing invalid instructions.
3. Role-Based Security (RBS):
- Grants or denies access based on the user’s identity and roles.
- Example: An admin can access certain modules, while a regular user cannot.
4. Cryptography Support:
- Provides built-in APIs in the System.Security.Cryptography namespace for encryption, hashing and secure data transmission.
Modern Development with .NET Ecosystem
While .NET Framework is still supported, Microsoft shifted towards .NET (Core/5/6/7) which is:
- Cross-platform (Windows, Linux, macOS).
- Open source and modular.
- Actively updated with modern features.
For new projects, .NET Core or .NET 5+ is recommended, while .NET Framework remains relevant for legacy enterprise applications.
Advantages
- Interoperability across multiple languages.
- Automatic memory management.
- Extensive class library.
- Security features like CAS and validation.
- Supports different types of applications.
Limitations
- Works only on Windows OS.
- Larger runtime footprint compared to lightweight platforms.
- Not cross-platform (unlike .NET Core/.NET 5+).