Overview
The System namespace is the root namespace in the .NET Framework and .NET. It contains the most fundamental types used in C# and .NET programming, including classes for basic data types (like integers, strings, and booleans), collections, memory management, error handling, and much more.
Key Concepts
- Base Types: The foundation for all other types in .NET.
- Value Types: Such as
int,float,bool,struct. - Reference Types: Such as
string,object,class. - Exception Handling: Classes like
Exception,ArgumentNullException. - Collections: Basic collection types and interfaces.
- Runtime Services: Classes for interacting with the .NET runtime.
Frequently Used Classes
Object
The ultimate base class of all C# types.
String
Represents a sequence of characters.
Console
Represents the standard input, output, and error streams for console applications.
Exception
The base class for all exceptions in the .NET Framework.
DateTime
Represents an instance of a date and time.
Common Topics
Value Types and Reference Types
Understanding the distinction between value and reference types.
Exception Handling
Best practices for handling errors using try-catch-finally blocks.
Garbage Collection
How memory is managed automatically in .NET.
Example Usage
Here's a simple example demonstrating basic types and output to the console:
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
// Declare and initialize variables
string message = "Hello, World!";
int number = 42;
bool isActive = true;
DateTime now = DateTime.Now;
// Output to console
Console.WriteLine(message);
Console.WriteLine("The answer is: " + number);
Console.WriteLine("Is active: " + isActive);
Console.WriteLine("Current date and time: " + now.ToString());
// Example of an exception
try
{
int zero = 0;
int result = 10 / zero; // This will throw a DivideByZeroException
}
catch (DivideByZeroException ex)
{
Console.WriteLine("An error occurred: " + ex.Message);
}
}
}