C# – Convert a dictionary to a list

The simplest way to convert a dictionary to a list is by using the Linq ToList() method, like this: When used on a dictionary, ToList() returns a list of KeyValuePair objects. This example outputs the following: Dictionary keys to list If you want a list of the dictionary’s keys, use ToList() on Dictionary.Keys, like this: … Read more

C# – How to use named tuples

You can use tuples to contain multiple values and pass them around. By default, the tuple field names are unfriendly – Item1, Item2, etc… Instead of using these default names, you can name the tuple fields. Here’s an example of creating and using a named tuple: Meaningful names makes tuples easier to use. movieTuple.title is … Read more

C# – Get the last day of the month

The last day of the month is the number of days in that month. To get the number of days in a month, use DateTime.DaysInMonth(year, month): This outputs the following: Notice that it handles leap years (2024) appropriately. Using the number of days in the month, you can get the last day of the month: … Read more

C# – Check if an IP range is valid

Given an IP range as a starting IP address and an ending IP address (as strings, like from user input or a config file), you can check if the IP range is valid by doing the following steps: Here’s an example. Let’s say you’re given starting IP “192.168.0.1” and ending “192.168.0.11”. The following table shows … Read more