How to Measure How Similar Two Vectors Are with TensorPrimitives.CosineSimilarity in C#
TensorPrimitives.CosineSimilarity(lemonade, orangeJuice) returns one number that says how alike two lists of numbers are: 1 when they point the same way, 0 when they're at a right angle, and -1 when they point in opposite directions. It comes from the System.Numerics.Tensors package.
A vector here is a list of numbers that describes something, such as a drink by how sweet, sour and bitter it is. Cosine similarity compares the directions that two vectors point in. Microsoft's reference for the method: Computes the cosine similarity between the two specified non-empty, equal-length tensors of single-precision floating-point numbers. A tensor is the library's name for such a list, and single-precision floating-point numbers are float values.
What the result means, from another page of the .NET reference: Cosine similarity measures only the angle between the two vectors, without taking into account the length of the vectors. And: -1 means vectors are opposite. 0 means vectors are orthogonal. Orthogonal means at a right angle.
Syntax
using System.Numerics.Tensors;
float <similarity> = TensorPrimitives.CosineSimilarity(<x>, <y>);
| Part | What it is |
|---|---|
using System.Numerics.Tensors; | The namespace of TensorPrimitives, from the System.Numerics.Tensors package |
<x>, <y> | The two vectors: float arrays with the same length and at least one number each |
<similarity> | A float from -1 to 1: 1 for the same direction, 0 for a right angle, -1 for the opposite direction |
The Example
A console app called CosineSimilarity first compares compass directions, written as two numbers each, to show the scale. Then it compares three drinks, each described by how sweet, sour and bitter it is, from 0 to 10.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n CosineSimilarity
cd CosineSimilarity
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/CosineSimilarity/CosineSimilarity.csproj:
Determining projects to restore...
Restored /Users/you/source/CosineSimilarity/CosineSimilarity.csproj (in 42 ms).
Restore succeeded.
dotnet new console -n CosineSimilarity creates a folder named CosineSimilarity with a project file and a Program.cs, and cd moves into it. This example needs the System.Numerics.Tensors package, which the start of Step 2 adds. Open the folder in VS Code, or CosineSimilarity.csproj in Visual Studio.
Step 2: Write the Code
First add the package. Run this in the CosineSimilarity folder:
dotnet add package System.Numerics.Tensors --version 10.0.15
info : X.509 certificate chain validation will use the fallback certificate bundle at '/usr/local/share/dotnet/sdk/10.0.404/trustedroots/codesignctl.pem'.
info : X.509 certificate chain validation will use the fallback certificate bundle at '/usr/local/share/dotnet/sdk/10.0.404/trustedroots/timestampctl.pem'.
info : Adding PackageReference for package 'System.Numerics.Tensors' into project '/Users/you/source/CosineSimilarity/CosineSimilarity.csproj'.
info : Restoring packages for /Users/you/source/CosineSimilarity/CosineSimilarity.csproj...
info : CACHE https://api.nuget.org/v3/vulnerabilities/index.json
info : CACHE https://api.nuget.org/v3-vulnerabilities/2026.11.30.23.39.07/vulnerability.base.json
info : CACHE https://api.nuget.org/v3-vulnerabilities/2026.11.30.23.39.07/2026.11.30.23.39.07/vulnerability.update.json
info : Package 'System.Numerics.Tensors' is compatible with all the specified frameworks in project '/Users/you/source/CosineSimilarity/CosineSimilarity.csproj'.
info : PackageReference for package 'System.Numerics.Tensors' version '10.0.15' added to file '/Users/you/source/CosineSimilarity/CosineSimilarity.csproj'.
info : Writing assets file to disk. Path: /Users/you/source/CosineSimilarity/obj/project.assets.json
log : Restored /Users/you/source/CosineSimilarity/CosineSimilarity.csproj (in 726 ms).
The command adds a PackageReference for System.Numerics.Tensors 10.0.15 to CosineSimilarity.csproj, then restores the project. Microsoft's reference on the command: After the project file is updated, dotnet restore is run. The same page notes a newer form of the command, dotnet package add: The "noun first" form was introduced in .NET 10.
Then replace everything in Program.cs. First the directions:
using System.Numerics.Tensors;
// The scale: the same direction gives 1, a right angle 0, and the opposite direction -1.
float[] east = [1, 0];
float[] farEast = [5, 0];
float[] north = [0, 1];
float[] west = [-1, 0];
Console.WriteLine($"east and farEast: {TensorPrimitives.CosineSimilarity(east, farEast)}");
Console.WriteLine($"east and north: {TensorPrimitives.CosineSimilarity(east, north)}");
Console.WriteLine($"east and west: {TensorPrimitives.CosineSimilarity(east, west)}");
using System.Numerics.Tensors; makes TensorPrimitives available. Each direction is a vector of two numbers, like a step on a map: east is 1 step right and north 1 step up. farEast points the same way as east, but is five times as long.
Then the drinks:
// Each array describes a drink by how sweet, sour and bitter it is, from 0 to 10.
float[] lemonade = [6, 8, 1];
float[] orangeJuice = [7, 6, 1];
float[] coffee = [1, 1, 9];
Console.WriteLine($"lemonade and orange juice: {TensorPrimitives.CosineSimilarity(lemonade, orangeJuice):F2}");
Console.WriteLine($"lemonade and coffee: {TensorPrimitives.CosineSimilarity(lemonade, coffee):F2}");
Each drink is a vector of three numbers in the same order: sweet, sour and bitter. :F2 prints the result with two decimals.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
east and farEast: 1
east and north: 0
east and west: -1
lemonade and orange juice: 0.98
lemonade and coffee: 0.25

east and farEast gave 1: they point the same way, and the length made no difference. east and north, at a right angle, gave 0, and east and west, opposite directions, gave -1. Lemonade and orange juice, both sweet and sour, scored 0.98, close to 1, while lemonade and the bitter coffee scored 0.25.
The Complete Files

using System.Numerics.Tensors;
// The scale: the same direction gives 1, a right angle 0, and the opposite direction -1.
float[] east = [1, 0];
float[] farEast = [5, 0];
float[] north = [0, 1];
float[] west = [-1, 0];
Console.WriteLine($"east and farEast: {TensorPrimitives.CosineSimilarity(east, farEast)}");
Console.WriteLine($"east and north: {TensorPrimitives.CosineSimilarity(east, north)}");
Console.WriteLine($"east and west: {TensorPrimitives.CosineSimilarity(east, west)}");
// Each array describes a drink by how sweet, sour and bitter it is, from 0 to 10.
float[] lemonade = [6, 8, 1];
float[] orangeJuice = [7, 6, 1];
float[] coffee = [1, 1, 9];
Console.WriteLine($"lemonade and orange juice: {TensorPrimitives.CosineSimilarity(lemonade, orangeJuice):F2}");
Console.WriteLine($"lemonade and coffee: {TensorPrimitives.CosineSimilarity(lemonade, coffee):F2}");
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Numerics.Tensors" Version="10.0.15" />
</ItemGroup>
</Project>
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Leaving out the package | error CS0234: The type or namespace name 'Tensors' does not exist in the namespace 'System.Numerics' (are you missing an assembly reference?) | Run dotnet add package System.Numerics.Tensors --version 10.0.15 first |
| Vectors of different lengths | ArgumentException: Input span arguments must all have the same length. | Give every vector the same numbers in the same order, such as sweet, sour and bitter |
int arrays | error CS0315: The type 'int' cannot be used as type parameter 'T' in the generic type or method 'TensorPrimitives.CosineSimilarity<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)'. There is no boxing conversion from 'int' to 'System.Numerics.IRootFunctions<int>'. | Use float arrays, or double |
| A vector of all zeros | Lemonade and water, [0, 0, 0], gave NaN, which means not a number: the formula divides by the vector's length, which is 0 | Leave out vectors that are all zeros: they have no direction to compare |
FAQ
How is the number worked out?
The reference says the method effectively computes TensorPrimitives.Dot(x, y) divided by MathF.Sqrt(TensorPrimitives.SumOfSquares(x)) times MathF.Sqrt(TensorPrimitives.SumOfSquares(y)). Dot multiplies the numbers in pairs and adds them up, and the square root of SumOfSquares is a vector's length. For lemonade and orange juice, a check printed a dot product of 91 and sums of squares of 101 and 86, and the formula gave 0.97640836, the same as CosineSimilarity.
Can I use double instead of float?
Yes. The .NET 9 release notes: the new CosineSimilarity<T>(ReadOnlySpan<T>, ReadOnlySpan<T>) overload performs cosine similarity on two vectors of float, double, or Half values. With double arrays, lemonade and orange juice gave 0.9764083389141934, and with float arrays 0.97640836. The reference also warns: Exact results may differ between different operating systems or architectures.
Conclusion
Add the System.Numerics.Tensors package, then call TensorPrimitives.CosineSimilarity with two vectors of the same length. The result says how alike their directions are, from 1 for the same direction through 0 to -1 for opposite ones, whatever the vectors' lengths.
Additional Resources
TensorPrimitives.CosineSimilarity— both forms, the formula and the exceptions.TensorPrimitives— the other operations on spans of numbers.- dotnet package add — adding a package to a project, in both forms of the command.
