.NET Core Fundamental Libraries
The .NET Core platform provides a rich set of fundamental libraries that form the backbone of application development. These libraries offer essential types and services for building robust and performant applications.
Key Namespace Overviews
System Namespace
The System namespace is the foundational namespace in .NET Core. It contains fundamental types that represent fundamental values and operations, including:
- Primitive data types (e.g.,
int,string,bool). - Base types for all .NET types (e.g.,
Object). - Common exceptions (e.g.,
ArgumentNullException). - Core functionalities like garbage collection, type conversion, and mathematical operations.
Example: Basic type conversion:
int number = 123;
string text = Convert.ToString(number);
Console.WriteLine(text); // Output: 123
System.Collections Namespace
This namespace provides interfaces and classes that define collections of objects. These include:
- Generic collections (e.g.,
List<T>,Dictionary<TKey, TValue>) for type-safe collections. - Non-generic collections (e.g.,
ArrayList,Hashtable) for backward compatibility. - Interfaces for defining custom collection types.
Example: Using a List<T>:
var names = new List<string>();
names.Add("Alice");
names.Add("Bob");
foreach (var name in names)
{
Console.WriteLine(name);
}
System.IO Namespace
The System.IO namespace provides types that allow reading and writing to files and data streams, as well as types that represent and manipulate various kinds of files and directories.
- Classes for file operations (e.g.,
File,Directory). - Classes for stream-based I/O (e.g.,
Stream,StreamReader,StreamWriter). - Classes for working with memory streams (e.g.,
MemoryStream).
Example: Reading from a file:
try
{
using (StreamReader reader = new StreamReader("example.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (FileNotFoundException)
{
Console.WriteLine("The file was not found.");
}
System.Net Namespace
This namespace provides a simple programming model for many of the data transmission protocols in the Internet protocol suite.
- Classes for network communication (e.g.,
HttpClient,TcpClient). - Classes for working with IP addresses and network endpoints.
- Support for common protocols like HTTP, FTP, and TCP.
Example: Making an HTTP GET request:
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync("https://www.example.com");
response.EnsureSuccessStatusCode(); // Throw if HTTP status is not 2xx
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody.Substring(0, 100) + "..."); // Print first 100 chars
}
System.Text Namespace
The System.Text namespace contains types that represent character encodings and classes that perform conversions between character encodings.
- Classes for encoding and decoding text (e.g.,
Encoding,UTF8Encoding). - Classes for working with strings and string builders (e.g.,
StringBuilder).
Example: Using StringBuilder for efficient string concatenation:
var builder = new StringBuilder();
for (int i = 0; i < 5; i++)
{
builder.Append("Item ").Append(i).Append(" ");
}
string result = builder.ToString();
Console.WriteLine(result); // Output: Item 0 Item 1 Item 2 Item 3 Item 4
System.Threading Namespace
This namespace provides types that support multi-threaded programming, including types that define and manage threads, synchronization primitives, and concurrent collections.
- Classes for managing threads (e.g.,
Thread,ThreadPool). - Synchronization primitives (e.g.,
Mutex,SemaphoreSlim,lockkeyword). - Asynchronous programming constructs (e.g.,
Task,CancellationToken).
Example: Starting a new thread:
void PrintMessage(object message)
{
Console.WriteLine(message);
}
Thread newThread = new Thread(PrintMessage);
newThread.Start("Hello from a new thread!");
Discover More
This is just a glimpse into the extensive .NET Core library ecosystem. For detailed information on specific classes, methods, and properties, please refer to the dedicated documentation pages for each namespace.
Frequently Used Types
| Namespace | Class/Type | Description |
|---|---|---|
System |
String |
Represents text as a sequence of characters. Immutable. |
System.Collections.Generic |
List<T> |
Represents a list of objects that can be accessed by index. Dynamically sized. |
System.IO |
File |
Provides static methods for the creation, copying, deletion, moving, and opening of files. |
System.Net.Http |
HttpClient |
Sends HTTP requests and receives HTTP responses from a resource identified by a URI. |
System.Text |
StringBuilder |
Represents a mutable sequence of characters. Efficient for multiple string manipulations. |
System.Threading.Tasks |
Task |
Represents an asynchronous operation. |