Learn to persist, transform, and query data with collections, LINQ, file I/O, and serialization techniques.
Welcome to our comprehensive guide on Collections and LINQ in C#. Mastering these topics will empower you to work effectively with data in your applications. Let’s dive into the core concepts, practical examples, and best practices!
Collections are data structures that allow you to store and manipulate multiple values. Unlike arrays, which have a fixed size and type, collections provide more flexibility and functionality.
// Example of different collections
int[] numbersArray = { 1, 2, 3 };
List<int> numbersList = new List<int> { 4, 5, 6 };
Dictionary<string, int> numberDict = new Dictionary<string, int> { { "one", 1 }, { "two", 2 } };
LINQ (Language Integrated Query) is a powerful querying tool that simplifies working with collections. It allows you to write readable, concise code for filtering, projecting, and aggregating data.
// LINQ Query Syntax
var evenNumbers = from n in numbersList
where n % 2 == 0
select n;
// LINQ Method Syntax
var evenNumbers = numbersList.Where(n => n % 2 == 0);
Use LINQ to filter data based on conditions and project only the necessary information.
// Filtering
var filtered = numbersList.Where(n => n > 3);
// Projection
var projected = numbersList.Select(n => new { DoubleValue = n * 2 });
Perform calculations on collections using aggregation methods like Sum(), Average(), Min(), Max().
// Sum of all numbers
int total = numbersList.Sum();
// Maximum value
int max = numbersList.Max();
Let’s see how collections and LINQ work together in a practical scenario: processing customer orders.
// Sample data
List<Order> orders = new List<Order>
{
new Order { Id = 1, CustomerName = "Alice", Amount = 200 },
new Order { Id = 2, CustomerName = "Bob", Amount = 300 },
new Order { Id = 3, CustomerName = "Charlie", Amount = 450 }
};
// LINQ query to calculate total sales for customers with amount > 250
var result = orders.Where(o => o.Amount > 250)
.Aggregate(0, (total, order) => total + order.Amount);
Console.WriteLine($"Total Sales: {result}");
Welcome to File I/O and Streams! In this chapter, you'll learn how to work with files in C# using the `File`, `StreamReader`, and `StreamWriter` classes. You'll discover how to read from and write to files while handling file paths efficiently and ensuring your code is safe from exceptions.
Let's start with the fundamentals of reading and writing files in C#.
// Reading a file
string text = File.ReadAllText(@"C:example.txt");
Console.WriteLine(text);
// Writing to a file
File.WriteAllText(@"C:output.txt", "Hello, World!");
For more advanced scenarios, especially when dealing with large files or memory constraints, you can use streams.
// Reading from a file using StreamReader
using (StreamReader reader = new StreamReader(@"C:example.txt")) {
string line;
while ((line = reader.ReadLine()) != null) {
Console.WriteLine(line);
}
}
// Writing to a file using StreamWriter
using (StreamWriter writer = new StreamWriter(@"C:output.txt")) {
writer.WriteLine("Hello, World!");
}
File operations can throw exceptions if files don't exist or permissions are denied. Here's how to handle them safely.
try {
string text = File.ReadAllText(@"C:example.txt");
} catch (FileNotFoundException) {
Console.WriteLine("The file was not found.");
} catch (UnauthorizedAccessException) {
Console.WriteLine("You don't have permission to access this file.");
} finally {
// Cleanup code if needed
}
Welcome to our chapter on JSON & XML Serialization! In this section, we'll explore how to serialize and deserialize data using both `System.Text.Json` and `Newtonsoft.Json`. You'll learn robust strategies for handling complex object graphs, null values, and schema changes.
The `System.Text.Json` namespace provides高性能和类型安全的 JSON 处理功能,适合现代 C# 开发。Newtonsoft.Json 作为社区驱动的替代方案,提供了额外的功能和兼容性。让我们看看如何使用它们:
using System.Text.Json;
var person = new Person { Name = "Alice", Age = 30 };
string json = JsonSerializer.Serialize(person);
Console.WriteLine(json);
When dealing with object graphs that contain nested objects, collections, or circular references, you need to use proper configuration options.
var settings = new JsonSerializerOptions
{
ReferenceHandling = ReferenceHandling.Preserve,
WriteIndented = true
};
Gracefully handling nulls is crucial for robust serialization. You can configure how null values are handled using the `NullValueHandling` option.
[ Newtonsoft.Json.JsonProperty(NullValueHandling = NullValueHandling.Ignore) ]
Serialization is essential in many aspects of application development, including: - API data exchange - Configuration management - Data persistence - Communication between services
public class Product
{
[XmlElement("id")]
public int Id { get; set; }
[XmlElement("name")]
public string Name { get; set; }
}
Question 1 of 13