Understanding Core Classes in .NET Core
Welcome to the section on Core Classes in .NET Core. This module explores the fundamental building blocks of the .NET ecosystem, providing a solid foundation for developing robust and efficient applications.
What are Core Classes?
Core classes, also known as Base Class Library (BCL) or Framework Classes, are pre-built components that provide essential functionalities for .NET development. These classes abstract away complex low-level operations, allowing developers to focus on application logic. They cover a wide range of areas, including:
- Data Structures: Collections like lists, dictionaries, and arrays.
- Input/Output (I/O): File system operations, network communication, and stream handling.
- String Manipulation: Operations for working with text data.
- Error Handling: Exception management and logging.
- Concurrency: Tools for managing multithreaded applications.
- Reflection: Inspecting and manipulating types and objects at runtime.
Key Namespace: System
The System
namespace is the root namespace for most of the fundamental classes in .NET Core. It contains types for basic data types, math operations, environment interaction, and more.
Commonly Used Types in System
:
String
: Represents sequences of characters.Int32
(orint
): Represents a 32-bit signed integer.Boolean
(orbool
): Represents a true or false value.DateTime
: Represents an instance of a date and time.Console
: Provides standard input, output, and error streams.
Example: Using Console and String
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
string name = "Developer";
Console.WriteLine($"Hello, {name}! Welcome to .NET Core.");
int number = 42;
Console.WriteLine($"The answer is: {number}");
}
}
Collections in System.Collections.Generic
Efficiently managing groups of objects is crucial. The System.Collections.Generic
namespace provides generic collection types that offer type safety and improved performance over non-generic collections.
Key Generic Collections:
List<T>
: A dynamically sized array.Dictionary<TKey, TValue>
: A collection of key-value pairs.HashSet<T>
: A collection of unique elements.Queue<T>
: A First-In, First-Out (FIFO) collection.Stack<T>
: A Last-In, First-Out (LIFO) collection.
Example: Using List<T>
using System;
using System.Collections.Generic;
public class ListExample
{
public static void Main(string[] args)
{
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Cherry");
Console.WriteLine("My favorite fruits:");
foreach (string fruit in fruits)
{
Console.WriteLine($"- {fruit}");
}
}
}
Error Handling with Exceptions
Robust applications must handle errors gracefully. .NET Core uses an exception-handling mechanism based on the System.Exception
class. You can catch specific exceptions and implement recovery logic.
try-catch-finally
blocks to manage potential exceptions. The finally
block ensures that cleanup code is executed, regardless of whether an exception occurred.
Example: Handling a DivideByZeroException
using System;
public class ExceptionHandling
{
public static void Main(string[] args)
{
try
{
int numerator = 10;
int denominator = 0;
int result = numerator / denominator;
Console.WriteLine($"Result: {result}"); // This line won't be reached
}
catch (DivideByZeroException ex)
{
Console.WriteLine($"Error: Cannot divide by zero. Details: {ex.Message}");
}
catch (Exception ex) // Catch any other general exceptions
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}
finally
{
Console.WriteLine("Exception handling example finished.");
}
}
}
File I/O with System.IO
The System.IO
namespace provides classes for reading from and writing to streams and files. Key classes include File
, Directory
, and StreamReader
/StreamWriter
.
Example: Writing to a File
using System;
using System.IO;
public class FileWriteExample
{
public static void Main(string[] args)
{
string filePath = "mydata.txt";
string content = "This is some data to write to the file.\nIt spans multiple lines.";
try
{
File.WriteAllText(filePath, content);
Console.WriteLine($"Successfully wrote to {filePath}");
// Example of reading from the file
string fileContent = File.ReadAllText(filePath);
Console.WriteLine($"\nContent of {filePath}:\n{fileContent}");
}
catch (Exception ex)
{
Console.WriteLine($"Error writing/reading file: {ex.Message}");
}
}
}
Next Steps
Understanding these core classes is vital for building any .NET Core application. Continue to the next section to learn about asynchronous programming, which is essential for writing responsive and scalable applications.
(To continue, please navigate to the Asynchronous Programming section.)