Using the new INumber type to generify math functions in .NET 7

.NET 7 is just around the corner and is already bringing a lot

Among those improvements, a small specific one is really elegant and may help you in writing methods that are dealing with numbers in a better way: the new INumber<T> interface.

.NET 7 is just around the corner and is already bringing a lot

Among those improvements, a small specific one is really elegant and may help you in writing methods that are dealing with numbers in a better way: the new INumber<T> interface.

Setup 🧰

This feature is still in preview so, to try and run the following code you will need to create a new project using .NET 7 and enable the preview features.

The .csproj for my console app looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <LangVersion>preview</LangVersion>
  </PropertyGroup>

</Project>

Use case 🔎

Let’s image that we are building a shopping app and we want to check out a cart. For now all prices TTC are round numbers so we wrote the following method to sum it all:

int CartValue(int[] numbers)
{
    var result = 0;
    foreach (var i in numbers) result += i;
    return result;
}

We later use it in our logic to compute the value of our cart:

var prices = new[] { 1, 2, 3 };
var sum = CartValue(prices);

And it works great !

Until …

It worked great but now we also have the shipment and other taxes that we must sum along with the price and our method no longer works:

var pricesWithTaxes = new[] { 1, 2, 3, 1.5 };
var sumWithTaxes = CartValue(pricesWithTaxes);
// ^ This is not an int[] and won't compile

We may be tempted to rewrite the CartValue method to accept double[] instead of int[] and then cast all numbers to double to fix this issue, but we also noticed that .NET 7 is available and there is a brand new feature for this

Introducing INumber

From now on, numbers are implementing the INumber<T> interface which exposes underlying math concepts such as the notion of zero, addition, one, etc. for numbers, regardless the type of T

In our case, it means that both int and double are INumber exposing the same concepts (although not the same values for those)

Let’s rewrite our method to accept an array of any number:

T CartValue<T>(T[] numbers)
    where T : INumber<T>  // <-- Don't forget the constraint!
{
    var result = T.Zero;
    foreach (var i in numbers) result += i;
    return result;
}

And just by using a generic to specify that the provided array is a number, our method is working great again and the previous code should now compile!

There are a lot more than just this interface in .NET 7 and I recommend you to check out the .NET 7 Preview blog that Microsoft has been writing about the next version of the framework

I hope that you learned something valuable, have a great day!


Print Share Comment Cite Upload Translate
APA
Pierre Bouillon | Sciencx (2024-03-29T12:59:53+00:00) » Using the new INumber type to generify math functions in .NET 7. Retrieved from https://www.scien.cx/2022/07/05/using-the-new-inumber-type-to-generify-math-functions-in-net-7/.
MLA
" » Using the new INumber type to generify math functions in .NET 7." Pierre Bouillon | Sciencx - Tuesday July 5, 2022, https://www.scien.cx/2022/07/05/using-the-new-inumber-type-to-generify-math-functions-in-net-7/
HARVARD
Pierre Bouillon | Sciencx Tuesday July 5, 2022 » Using the new INumber type to generify math functions in .NET 7., viewed 2024-03-29T12:59:53+00:00,<https://www.scien.cx/2022/07/05/using-the-new-inumber-type-to-generify-math-functions-in-net-7/>
VANCOUVER
Pierre Bouillon | Sciencx - » Using the new INumber type to generify math functions in .NET 7. [Internet]. [Accessed 2024-03-29T12:59:53+00:00]. Available from: https://www.scien.cx/2022/07/05/using-the-new-inumber-type-to-generify-math-functions-in-net-7/
CHICAGO
" » Using the new INumber type to generify math functions in .NET 7." Pierre Bouillon | Sciencx - Accessed 2024-03-29T12:59:53+00:00. https://www.scien.cx/2022/07/05/using-the-new-inumber-type-to-generify-math-functions-in-net-7/
IEEE
" » Using the new INumber type to generify math functions in .NET 7." Pierre Bouillon | Sciencx [Online]. Available: https://www.scien.cx/2022/07/05/using-the-new-inumber-type-to-generify-math-functions-in-net-7/. [Accessed: 2024-03-29T12:59:53+00:00]
rf:citation
» Using the new INumber type to generify math functions in .NET 7 | Pierre Bouillon | Sciencx | https://www.scien.cx/2022/07/05/using-the-new-inumber-type-to-generify-math-functions-in-net-7/ | 2024-03-29T12:59:53+00:00
https://github.com/addpipe/simple-recorderjs-demo