Comparing two lists for differences is a common requirement in C# development, especially when working with data synchronization, validation, or processing changes between datasets.
The .NET Framework offers several elegant approaches to identify these differences efficiently, from built-in LINQ methods to more specialized comparison techniques depending on your specific needs.
A straightforward approach uses LINQ's Except() and Intersect() methods to find elements that exist in one list but not the other.
Except()
Intersect()
For example, if you have two lists of integers:
using System; using System.Collections.Generic; using System.Linq; public class ListComparer { public static void Main() { List<int> firstList = new List<int> { 1, 2, 3, 4, 5 }; List<int> secondList = new List<int> { 3, 4, 5, 6, 7 }; // Items in first list but not in second var onlyInFirst = firstList.Except(secondList).ToList(); Console.WriteLine("Only in first list: " + string.Join(", ", onlyInFirst)); // Items in second list but not in first var onlyInSecond = secondList.Except(firstList).ToList(); Console.WriteLine("Only in second list: " + string.Join(", ", onlyInSecond)); // Items in both lists var inBoth = firstList.Intersect(secondList).ToList(); Console.WriteLine("In both lists: " + string.Join(", ", inBoth)); } }
For comparing lists of complex objects, you'll need to implement IEqualityComparer<T> or use more sophisticated approaches like object diffing libraries such as CompareNETObjects.
IEqualityComparer<T>
This approach gives you fine-grained control over which properties are considered during comparison, making it ideal for identifying specific differences in business objects or entity models.
Closing a SqlDataReader correctly prevents memory leaks, connection issues, and unclosed resources. Here’s the best way to do it.
Using using statements ensures SqlDataReader and SqlConnection are closed even if an exception occurs.
Example
using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users", conn)) using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Console.WriteLine(reader["Username"]); } } // ✅ Auto-closes reader here } // ✅ Auto-closes connection here
This approach auto-closes resources when done and it is cleaner and less error-prone than manual closing.
If you need explicit control, you can manually close it inside a finally block.
SqlDataReader? reader = null; try { using SqlConnection conn = new SqlConnection(connectionString); conn.Open(); using SqlCommand cmd = new SqlCommand("SELECT * FROM Users", conn); reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader["Username"]); } } finally { reader?.Close(); // ✅ Closes reader if it was opened }
This is slightly more error prone if you forget to add a finally block. But might make sense when you need to handle the reader separately from the command or connection.
Storing passwords as plain text is dangerous. Instead, you should hash them using a strong, slow hashing algorithm like BCrypt, which includes built-in salting and resistance to brute-force attacks.
Step 1: Install BCrypt NuGet Package
Before using BCrypt, install the BCrypt.Net-Next package:
dotnet add package BCrypt.Net-Next
or via NuGet Package Manager:
Install-Package BCrypt.Net-Next
Step 2: Hash a Password
Use BCrypt.HashPassword() to securely hash a password before storing it:
using BCrypt.Net; string password = "mySecurePassword123"; string hashedPassword = BCrypt.HashPassword(password); Console.WriteLine(hashedPassword); // Output: $2a$12$...
Step 3: Verify a Password
To check a user's login attempt, use BCrypt.Verify():
bool isMatch = BCrypt.Verify("mySecurePassword123", hashedPassword); Console.WriteLine(isMatch); // Output: True
Ensuring proper hashing should be at the top of your list when it comes to building authentication systems.
Raw string literals in C# provide a flexible way to work with multiline strings, with some interesting rules around how quotes work.
The key insight is that you can use any number of double quotes (three or more) to delimit your string, as long as the opening and closing sequences have the same number of quotes.
"""
// Three quotes - most common usage string basic = """ This is a basic multiline string """; // Four quotes - when your content has three quotes string withThreeQuotes = """" Here's some text with """quoted""" content """"; // Five quotes - when your content has four quotes string withFourQuotes = """"" Here's text with """"nested"""" quotes """""; // Six quotes - for even more complex scenarios string withFiveQuotes = """""" Look at these """""nested""""" quotes! """""";
The general rule is that if your string content contains N consecutive double quotes, you need to wrap the entire string with at least N+1 quotes. This ensures the compiler can properly distinguish between your content and the string's delimiters.
// Example demonstrating the N+1 rule string example1 = """ No quotes inside """; // 3 quotes is fine string example2 = """" Contains """three quotes""" """"; // Needs 4 quotes (3+1) string example3 = """"" Has """"four quotes"""" """""; // Needs 5 quotes (4+1)
// Indentation example string properlyIndented = """ { "property": "value", "nested": { "deeper": "content" } } """; // This line's position determines the indentation
This flexibility with quote counts makes raw string literals extremely versatile, especially when dealing with content that itself contains quotes, like JSON, XML, or other structured text formats.
Register for my free weekly newsletter.