.NET Fundamentals
What is .NET?
.NET is a free, cross-platform, open-source developer platform for building many different types of applications. With .NET, you can:
- Build web apps, services, and APIs with ASP.NET Core.
- Build mobile apps with .NET MAUI.
- Build desktop apps with WPF, Windows Forms, and .NET MAUI.
- Build games with Unity.
- Build backend services, IoT apps, machine learning models, and more.
It's a developer platform that includes tools, languages, and libraries for building applications.
Key .NET Components
Understanding the core components of the .NET ecosystem is crucial for effective development.
Runtime (CLR)
The Common Language Runtime (CLR) is the execution engine of .NET. It provides core services such as:
- Memory Management: Automatic garbage collection to free up memory.
- Thread Management: Support for multithreading and concurrency.
- Exception Handling: Robust mechanisms for handling errors.
- Just-In-Time (JIT) Compilation: Compiles intermediate language (IL) code into native machine code at runtime.
Base Class Library (BCL)
The Base Class Library (BCL) provides a set of fundamental types and functionalities that developers can use to build applications. This includes:
- Collections: Data structures like lists, dictionaries, and arrays.
- I/O: File system access, network communication, and streams.
- Networking: Support for various network protocols.
- Serialization: Converting objects into formats that can be stored or transmitted.
- Threading: Utilities for managing concurrent operations.
Languages
.NET supports multiple programming languages, with C# being the most prominent. Other supported languages include F# and Visual Basic.
- C#: A modern, object-oriented, and type-safe programming language.
- F#: A functional-first, multi-paradigm programming language designed for robust code.
- Visual Basic: A language that allows you to leverage the power of .NET while being familiar to previous Visual Basic developers.
Common Intermediate Language (CIL)
Source code written in .NET languages is compiled into Common Intermediate Language (CIL), also known as Microsoft Intermediate Language (MSIL). CIL is a CPU-agnostic, platform-agnostic instruction set that the CLR then compiles into native machine code.
.NET Standard
.NET Standard is a formal specification of .NET APIs that are intended to be available on all .NET implementations. It acts as a contract to ensure that libraries built for .NET Standard can run on any .NET implementation that supports that version of the Standard.
Common Tasks and Examples
Here are some basic examples of common tasks in .NET.
Hello, World! in C#
A simple console application example:
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, .NET World!");
}
}
Using Collections
Demonstrates using a List from the BCL:
using System;
using System.Collections.Generic;
public class CollectionExample
{
public static void Main(string[] args)
{
List names = new List();
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");
Console.WriteLine("Names:");
foreach (string name in names)
{
Console.WriteLine($"- {name}");
}
}
}