C# – Examples of using GroupBy()

Here’s an example of using GroupBy() to group coders by language: This example outputs the following: GroupBy() produces groups that contain the grouping key (i.e. Language) and the list of objects in the group (i.e. the Coder objects). The GroupBy() syntax is complex because it supports many scenarios. You can select one or more grouping … Read more

C# – How to read response headers with HttpClient

When you send a request with HttpClient, it returns an HttpResponseMessage. You can read the response headers through the HttpResponseMessage.Headers property: This outputs the response headers: Raw response headers are really just key/value(s) pairs. When the response comes in, the headers are loaded into the Headers property (which is of type HttpResponseHeaders). This parses the … Read more

C# – Save a list of strings to a file

The simplest way to save a list of strings to a file is to use File.WriteAllLines(). This creates (or overwrites) the specified file and writes each string on a new line. The resulting file looks like this: Note: Showing non-printable newline characters \r\n for clarity. Specifying the separator character What if you want to separate … Read more

C# – Remove non-alphanumeric characters from a string

The simplest way to remove non-alphanumeric characters from a string is to use regex: Note: Don’t pass in a null, otherwise you’ll get an exception. Regex is the simplest approach for solving this problem, but it’s also the slowest. If you’re concerned about the performance take a look at the performance section below. This example … Read more

C# – Get all classes with a custom attribute

To get all classes with a custom attribute, first get all types in the assembly, then use IsDefined(customAttributeType) to filter the types: This is looking for classes in the current assembly that have the [ApiController] attribute, such as this controller class: This is useful in several scenarios, such as when you want to log information … Read more

C# – Convert a list to a dictionary

The simplest way to convert a list to a dictionary is to use the Linq ToDictionary() method: This loops through the list and uses the key/element selector lambdas you passed in to build the dictionary. In this article, I’ll go into details about how to use ToDictionary() and show how to deal with duplicate keys. … Read more

C# – Select distinct objects based on a property

There are three ways to select distinct objects based on a property using Linq: These select one movie per year: GroupBy() is the simplest because it doesn’t require any additional code. Distinct() is faster but it’s more complicated. DistinctBy() is the fastest and simplest, but requires the most code (it requires .NET 6 or getting … Read more

C# – Search XML for elements, values, and attributes

The simplest way to search XML is to use the Linq-to-XML API in .NET (in System.Xml.Linq). This parses XML and allows you to perform Linq queries on it. Here’s an example: This outputs: XElement is the main class. You use it in combination with Linq methods to perform searches. In this article, I’ll how to … Read more

C# – Check if a string contains any substring from a list

There are many different scenarios where you might want to check a string against a list of substrings. Perhaps you’re dealing with messy exception handling and have to compare the exception message against a list of known error messages to determine if the error is transient or not. When you need to check a string … Read more

System.InvalidOperationException: Collection was modified; enumeration operation may not execute

If you try to add/remove items from a collection while it’s being looped over in a foreach loop (enumerated), then you’ll get the following exception: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.at System.Collections.Generic.List`1.Enumerator.MoveNext() This error can happen in two scenarios: You’re looping over the collection in a foreach loop and modifying it (add/removing) … Read more