Using Relational Patterns in C# & .NET
[C#, .NET]
Boolean logic is at the heart of many algorithms that you implement or use in the course of writing software.
Take, for example, this type:
public record Person(string FullName, DateOnly DateOfBirth, string HomeTown, Gender Gender);
Gender is an enum that is defined thus:
public enum Gender
{
Male,
Famale
}
Suppose we wanted to process Person types based on the following criteria:
We want to recruit spies born between 1950 and 1965.
The code would typically look like this:
void Process(Person person)
{
if (person.DateOfBirth.Year >= 1950 && person.DateOfBirth.Year <= 1965)
Console.WriteLine($"{person.FullName} is eligible!");
}
Next, we create a bunch of Person objects:
Person[] people =
[
new Person("James Bond", new DateOnly(1960, 1, 1), "London", Gender.Male),
new Person("Harry Pearce", new DateOnly(1955, 1, 1), "Cryodon", Gender.Male),
new Person("Evelyn Salt", new DateOnly(1960, 1, 1), "Nairobi", Gender.Famale),
new Person("Vesper Lynd", new DateOnly(1970, 1, 1), "Mombasa", Gender.Famale)
];
Finally, we process them:
foreach (var person in people)
{
Process(person);
}
This should print the following:

Now let us take a closer look at the logic:
if (person.DateOfBirth.Year >= 1950 && person.DateOfBirth.Year <= 1965)
Console.WriteLine($"{person.FullName} is eligible!");
This can be simplified as follows:
if (person.DateOfBirth.Year is >= 1950 and <= 1965)
Console.WriteLine($"{person.FullName} is eligible!");
Note the following improvements:
- We no longer keep repeating
person.DateOfBirth.Year - The logic is easier to read:
person.DateOfBirth.Year is >= 1950 and <= 1965vsperson.DateOfBirth.Year >= 1950 && person.DateOfBirth.Year <= 1965
This is called a relational pattern.
You can also write it like this, a feature known as an object pattern.
if (person.DateOfBirth is { Year: >= 1950 and <= 1965 })
Console.WriteLine($"{person.FullName} is eligible!");
I, however, personally find this harder to read.
TLDR
Relational patterns can be used to simplify conditional logic.
The code is in my GitHub.
Happy hacking!