Standard Libraries in C#
Introduction
Table of Contents
When you start learning C#, one of the most powerful tools at your disposal is the Standard Library, also known as the .NET Base Class Library (BCL). Think of it as a giant toolbox filled with pre-built tools that help you write programs faster and more efficiently. Instead of building everything from scratch, you can use these tools to handle common tasks like working with text, files, numbers, dates, and even more advanced things like networking or multithreading.
The standard libraries in C# are organized into namespaces, which are like folders that group related tools together. For example:
- System is the main namespace and contains basic tools like data types (int, string, etc.) and math operations.
- System.IO helps you work with files and folders.
- System.Collections.Generic provides tools for managing lists, dictionaries, and other collections of data.
As a beginner, you don’t need to learn everything at once. Start with the basics, like understanding how to use strings, numbers, and collections. Then, as you grow more comfortable, you can explore more advanced libraries for tasks like reading files, making web requests, or handling multiple tasks at the same time.
The best part? These libraries are built into the .NET framework, so you don’t need to install anything extra. They’re always there, ready to help you build amazing applications.
Here are some commonly used namespaces in C#:
Namespace | Purpose |
System | Core types like Console, Math, DateTime, and basic operations |
System.IO | File handling (StreamReader, StreamWriter, File, Directory) |
System.Collections.Generic | Generic collections (List<T>, Dictionary<K,V>, Queue<T>) |
System.Linq | Query operations on collections (Where(), Select(), OrderBy()) |
System.Text | Text manipulation, including StringBuilder |
System.Threading | Multithreading and parallel processing |
System.Net.Http | Handling HTTP requests and responses |
System.Data | Database connectivity (SqlConnection, SqlCommand) |
Objectives
When diving into the world of C# and its standard libraries, it’s important to have clear goals in mind. These libraries are a cornerstone of the language, and mastering them will empower you to write efficient, clean, and powerful code. Below are four key objectives to guide your learning journey: Understand, Learn, Practice, and Apply. These steps will help you build a strong foundation and gradually become confident in using the .NET Base Class Library (BCL) to solve real-world problems.
- Understand
- Gain a clear understanding of what standard libraries are and how they are organized in C#.
- Familiarize yourself with the purpose of key namespaces like System, System.Collections.Generic, and System.IO.
- Recognize the importance of using pre-built tools to save time and effort in coding.
- Learn
- Explore the most commonly used classes and methods in the standard libraries.
- Learn how to work with basic data types, collections, files, and other essential features.
- Understand the documentation and how to find the right tools for specific tasks.
- Practice
- Write small programs to experiment with the libraries.
- Solve coding exercises that involve using standard library features.
- Debug and refine your code to gain hands-on experience.
- Apply
- Use the standard libraries to build real-world projects, such as a file manager, a simple web scraper, or a task scheduler.
- Integrate multiple libraries to create more complex and functional applications.
- Develop the confidence to use the libraries effectively in your own projects.
Source code example
Example 1: Using System (Console & Math Operations)
using System; class Program { static void Main() { Console.WriteLine("Enter a number:"); int number = int.Parse(Console.ReadLine()); Console.WriteLine($"Square Root: {Math.Sqrt(number)}"); Console.WriteLine($"Power of 2: {Math.Pow(number, 2)}"); Console.ReadKey(); } }
Example 2: Using System.IO (File Handling)
using System; using System.IO; class Program { static void Main() { string path = "example.txt"; File.WriteAllText(path, "Hello, C#!"); string content = File.ReadAllText(path); Console.WriteLine($"File Content: {content}"); } }
Example 3: Using System.Collections.Generic (List and Dictionary)
using System; using System.Collections.Generic; class Program { static void Main() { List<string> names = new List<string> { "Alice", "Bob", "Charlie" }; names.Add("David"); Dictionary<int, string> students = new Dictionary<int, string> { { 1, "Alice" }, { 2, "Bob" } }; Console.WriteLine($"First name: {names[0]}"); Console.WriteLine($"Student with ID 1: {students[1]}"); } }
Example 4: Using System.Linq (Filtering Data)
using System; using System.Linq; using System.Collections.Generic; class Program { static void Main() { List<int> numbers = new List<int> { 10, 20, 30, 40, 50 }; var filtered = numbers.Where(n => n > 20).ToList(); Console.WriteLine("Numbers greater than 20:"); filtered.ForEach(n => Console.WriteLine(n)); } }
Summary
C# standard libraries provide essential tools that simplify coding by offering prebuilt functions for handling input/output, collections, and data processing. In this lesson, you learned to understand their purpose, learn how to use key namespaces, practice with hands-on exercises, and apply these libraries in real-world scenarios. By using built-in functionalities, you can write more efficient and maintainable code, making programming in C# faster and easier. Mastering these libraries will help you develop high-quality applications with less effort.
Quiz
- Remembering: What is the purpose of C# standard libraries?
A. To replace user-defined functions
B. To provide prewritten code for common tasks
C. To slow down program execution
D. To prevent code reuse - Understanding: Which namespace is commonly used for handling file input and output in C#?
A. System.Collections.Generic
B. System.Text
C. System.IO
D. System.Linq - Applying: If you need to store a list of unique student IDs and retrieve them quickly, which C# collection should you use?
A. List<int>
B. Dictionary<int, string>
C. Queue<int>
D. Array<int> - Analyzing: What will happen if you use File.ReadAllText(“data.txt”) without checking if the file exists?
A. It will return an empty string
B. It will create a new file named data.txt
C. It will throw a FileNotFoundException
D. It will automatically generate file content - Evaluating: Why is using System.Linq beneficial for handling collections in C#?
A.It increases memory usage
B. It allows writing complex operations in a simpler, more readable way
C. It removes all elements from a list
D. It only works with arrays and not lists
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.