Introduction to .NET Core Concepts
.NET is a free, cross-platform, open-source developer platform for building many different types of applications. With .NET, you can use C#, F#, or Visual Basic to create applications for Windows, macOS, Linux, Android, iOS, and more.
This documentation provides an overview of the fundamental concepts that underpin the .NET platform, essential for any developer working with .NET technologies.
The .NET Runtime (Common Language Runtime - CLR)
The Common Language Runtime (CLR) is the execution engine of .NET. It provides the managed execution environment that intermediates between the operating system and the application code. Key features of the CLR include:
- Just-In-Time (JIT) Compilation: Compiles intermediate language (IL) code into native machine code at runtime, optimizing performance.
- Garbage Collection (GC): Automatically manages memory allocation and deallocation, preventing memory leaks.
- Type Safety: Enforces type checking to prevent common programming errors.
- Exception Handling: Provides a structured way to handle runtime errors.
- Security: Offers robust security features for application execution.
Managed code is code that is executed by the CLR.
.NET Framework Libraries (Base Class Library - BCL)
The Base Class Library (BCL) is a comprehensive set of pre-written classes and types that provide a rich set of functionalities for application development. It includes classes for:
- Collections
- File I/O
- Networking
- Database Access
- Threading
- XML Processing
- String Manipulation
- And much more.
Developers leverage the BCL to build applications efficiently without having to implement common functionalities from scratch.
Programming Languages
.NET supports multiple programming languages, with C#, F#, and Visual Basic being the primary ones. These languages are compiled into a common intermediate language (IL) and executed by the CLR, enabling interoperability between them.
- C#: A modern, object-oriented, and type-safe programming language.
- F#: A functional-first, cross-platform language that also supports object-oriented and imperative programming.
- Visual Basic (.NET): A robust and productive language with a simple syntax.
Assemblies
An assembly is the fundamental unit of deployment, versioning, reuse, activation, and security for .NET applications. It is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies are typically deployed as one or more files with a .dll
or .exe
extension.
Every assembly contains a manifest that describes its contents, versioning information, and security requirements.
Namespaces
Namespaces are used to organize types and provide a hierarchical structure to the .NET Framework Libraries. They help prevent naming conflicts and make it easier to manage large codebases. For example, the System.Collections
namespace contains classes for various collection types.
You use the using
directive to bring types from a namespace into the current scope:
using System;
using System.Collections.Generic;
public class MyClass
{
public static void Main(string[] args)
{
List<string> names = new List<string>();
names.Add("Alice");
Console.WriteLine($"Hello, {names[0]}!");
}
}
Types (Classes, Structs, Interfaces, Enums, Delegates)
.NET is built around the concept of types, which define the data and behavior of objects. The primary type categories include:
- Classes: Blueprints for creating objects, supporting inheritance and encapsulation.
- Structs: Value types, typically used for small data structures.
- Interfaces: Contracts that define a set of members (methods, properties) that a class or struct must implement.
- Enums: Define a set of named constants.
- Delegates: Type-safe function pointers, used for event handling and callbacks.
Object-Oriented Programming (OOP)
.NET strongly supports OOP principles:
- Encapsulation: Bundling data (fields) and methods that operate on the data within a single unit (class), and restricting access to the data.
- Inheritance: Allowing a new class (derived class) to inherit properties and methods from an existing class (base class).
- Polymorphism: The ability of an object to take on many forms, often through method overriding or interfaces.
- Abstraction: Hiding complex implementation details and exposing only essential features.
Generics
Generics allow you to define type-safe collections and methods that can operate on any type without sacrificing performance or type safety. For example, List<T>
is a generic collection class where T
is a type parameter.
// Generic list of strings
List<string> stringList = new List<string>();
stringList.Add("Hello");
// Generic list of integers
List<int> intList = new List<int>();
intList.Add(123);
Exception Handling
.NET uses a structured exception handling mechanism with try
, catch
, and finally
blocks to manage runtime errors gracefully.
try
{
// Code that might throw an exception
int result = 10 / 0;
}
catch (DivideByZeroException ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
finally
{
// Code that always executes
Console.WriteLine("Cleanup operations.");
}
Memory Management (Garbage Collection)
The CLR's Garbage Collector (GC) automatically reclaims memory occupied by objects that are no longer in use. This simplifies development by relieving programmers from manual memory management, reducing the risk of memory leaks and dangling pointers.
The GC operates in cycles, identifying and freeing unused objects.
Asynchronous Programming
.NET provides powerful support for asynchronous programming using the async
and await
keywords. This enables applications to remain responsive by performing long-running operations on background threads, without blocking the main execution thread.
public async Task<string> GetDataFromApiAsync()
{
using (HttpClient client = new HttpClient())
{
string result = await client.GetStringAsync("https://api.example.com/data");
return result;
}
}