Absolutely! Let’s go through using .NET built-in libraries in C# — powerful collections of classes and functions that simplify common programming tasks.
📌 1. System.IO
– Input/Output
-
Provides classes for working with files, directories, and streams
-
Commonly used classes:
-
File
→ high-level file operations (ReadAllText
,WriteAllText
) -
FileInfo
→ detailed file information and manipulation -
Directory
→ create, delete, and enumerate directories -
StreamReader
/StreamWriter
→ reading/writing text files -
FileStream
→ low-level byte-based file access
-
Example: Reading a File
using System.IO;
string content = File.ReadAllText("example.txt");
Console.WriteLine(content);
Example: Writing a File
File.WriteAllText("output.txt", "Hello, World!");
📌 2. System.Text
– Text Processing
-
Provides classes for encoding, string building, and regular expressions
-
Commonly used classes:
-
StringBuilder
→ efficient string concatenation -
Encoding
→ convert strings to bytes and vice versa -
Regex
→ regular expressions for pattern matching
-
Example: Using StringBuilder
using System.Text;
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
sb.Append(" ");
sb.Append("World!");
Console.WriteLine(sb.ToString()); // Hello World!
Example: Using Encoding
byte[] bytes = Encoding.UTF8.GetBytes("Hello");
string text = Encoding.UTF8.GetString(bytes);
Console.WriteLine(text);
📌 3. System.Collections
– Collections
-
Provides basic collection classes (non-generic):
-
ArrayList
→ dynamically sized array -
Hashtable
→ key-value storage -
Queue
,Stack
→ FIFO and LIFO collections
-
-
For type-safe collections, prefer System.Collections.Generic
Example: Using ArrayList
using System.Collections;
ArrayList list = new ArrayList();
list.Add(1);
list.Add("Hello");
foreach (var item in list)
{
Console.WriteLine(item);
}
📌 4. Why Use Built-in Libraries
Library | Purpose |
---|---|
System.IO | File, directory, and stream operations |
System.Text | Efficient string handling, encoding, regex |
System.Collections | Basic collections, queues, stacks, hashtables |
-
Speeds up development
-
Provides tested, optimized functionality
-
Reduces need for manual implementations
📌 5. Best Practices
-
Prefer generic collections (
List<T>
,Dictionary<K,V>
) overArrayList
andHashtable
-
Use
StringBuilder
when concatenating strings in loops -
Always dispose streams using
using
blocks to release resources -
Use System.Text.RegularExpressions.Regex for complex pattern matching
📌 6. Summary Table
Namespace | Common Classes | Example Use Case |
---|---|---|
System.IO | File, StreamReader, FileStream | Reading/writing files |
System.Text | StringBuilder, Encoding, Regex | String manipulation, encoding, pattern matching |
System.Collections | ArrayList, Hashtable, Queue, Stack | Simple non-generic collections |
✅ Tip:
-
Built-in libraries are the backbone of C# development — learn them well.
-
For modern applications, prefer generic collections from
System.Collections.Generic
for type safety and performance.
I can also make a diagram showing System.IO for file operations, System.Text for string/encoding, and System.Collections for collections — visually connecting their roles.
Do you want me to create that diagram?