C# System Library
Core functionality for C# applications
📚 What is the System Library?
The System library is the foundation of C# programming. It provides essential classes for console operations, data types, math functions, and basic program functionality that every C# application needs.
// Simple System library usage
using System;
Console.WriteLine("Hello, C#!");
Output:
Hello, C#!
Core System Features
Console I/O
Read and write to console
Console.WriteLine("Output");
Console.ReadLine();
Math Operations
Mathematical calculations
Math.Max(10, 20);
Math.Sqrt(16);
DateTime
Work with dates and times
DateTime now = DateTime.Now;
DateTime.Today;
String Methods
Manipulate text data
string.ToUpper();
string.Contains("text");
🔹 Console Input and Output
The Console class in C# provides simple methods for reading user input and displaying output in command-line applications. Using Console.ReadLine() captures text entered by the user, while Console.WriteLine() prints messages to the terminal. For instance, a program can prompt for a name, store the input, and respond with a personalized greeting. This is ideal for learning basic I/O operations, building interactive scripts, and debugging through direct console feedback.
using System;
class Program
{
static void Main()
{
// Output to console
Console.WriteLine("What is your name?");
// Read user input
string name = Console.ReadLine();
// Display personalized message
Console.WriteLine("Hello, " + name + "!");
}
}
Output:
What is your name?
John
Hello, John!
🔹 Math Class
C#’s Math class offers a comprehensive set of static methods for performing common mathematical operations. It includes functions like Math.Max(), Math.Min(), Math.Sqrt(), Math.Pow(), and Math.Round(), which are essential for calculations in games, data analysis, and scientific applications. For example, you can easily compute the square root of a number or round a decimal value. These built-in methods enhance code readability, precision, and performance without requiring manual implementations.
using System;
class Program
{
static void Main()
{
// Basic math operations
int max = Math.Max(15, 25);
int min = Math.Min(15, 25);
// Square root and power
double sqrt = Math.Sqrt(64);
double power = Math.Pow(2, 3);
// Rounding
double rounded = Math.Round(4.7);
Console.WriteLine($"Max: {max}, Min: {min}");
Console.WriteLine($"Square root of 64: {sqrt}");
Console.WriteLine($"2 to power 3: {power}");
Console.WriteLine($"4.7 rounded: {rounded}");
}
}
Output:
Max: 25, Min: 15
Square root of 64: 8
2 to power 3: 8
4.7 rounded: 5
🔹 DateTime Operations
The DateTime structure in C# enables efficient manipulation and formatting of dates and times. You can retrieve the current date and time with DateTime.Now, add or subtract days using AddDays(), and format outputs into strings like "yyyy-MM-dd". This is crucial for scheduling features, timestamping events, or calculating intervals. For instance, you can determine a date one week from today and display it in a user-friendly format, simplifying complex temporal logic.
using System;
class Program
{
static void Main()
{
// Current date and time
DateTime now = DateTime.Now;
DateTime today = DateTime.Today;
// Add days
DateTime nextWeek = now.AddDays(7);
// Format date
string formatted = now.ToString("yyyy-MM-dd");
Console.WriteLine($"Now: {now}");
Console.WriteLine($"Today: {today}");
Console.WriteLine($"Next week: {nextWeek}");
Console.WriteLine($"Formatted: {formatted}");
}
}
Output:
Now: 10/6/2025 2:30:45 PM
Today: 10/6/2025 12:00:00 AM
Next week: 10/13/2025 2:30:45 PM
Formatted: 2025-10-06
🔹 String Manipulation
String manipulation in C# includes methods to transform, search, and split text efficiently. Common operations like converting to upper/lower case with ToUpper() and ToLower(), checking for substrings via Contains(), replacing text with Replace(), and splitting into arrays with Split() are foundational for text processing. These capabilities support tasks such as data validation, formatting user input, parsing files, and generating dynamic content in applications.
using System;
class Program
{
static void Main()
{
string text = "Hello World";
// Change case
string upper = text.ToUpper();
string lower = text.ToLower();
// Check content
bool contains = text.Contains("World");
// Replace text
string replaced = text.Replace("World", "C#");
// Split string
string[] words = text.Split(' ');
Console.WriteLine($"Upper: {upper}");
Console.WriteLine($"Lower: {lower}");
Console.WriteLine($"Contains 'World': {contains}");
Console.WriteLine($"Replaced: {replaced}");
Console.WriteLine($"First word: {words[0]}");
}
}
Output:
Upper: HELLO WORLD
Lower: hello world
Contains 'World': True
Replaced: Hello C#
First word: Hello
🔹 Type Conversion
Type conversion in C# allows safe transformation between data types like strings, integers, and decimals. Methods such as Convert.ToInt32(), ToString(), decimal.Parse(), and int.TryParse() facilitate handling user input, file data, and API responses. For example, TryParse() safely converts a string to a number without throwing exceptions. Proper type conversion ensures data integrity, prevents runtime errors, and enables seamless interaction between different parts of a program.
using System;
class Program
{
static void Main()
{
// String to number
string numText = "123";
int number = Convert.ToInt32(numText);
// Number to string
int value = 456;
string text = value.ToString();
// Parse methods
double price = double.Parse("19.99");
// TryParse (safe conversion)
bool success = int.TryParse("789", out int result);
Console.WriteLine($"Converted number: {number}");
Console.WriteLine($"Number as text: {text}");
Console.WriteLine($"Parsed price: {price}");
Console.WriteLine($"TryParse result: {result}");
}
}
Output:
Converted number: 123
Number as text: 456
Parsed price: 19.99
TryParse result: 789