-
Book Overview & Buying
-
Table Of Contents
Web Development with Blazor - Fourth Edition
By :
Since we are running WebAssembly, we can use WebAssembly assemblies written in other languages in our project. This means that we can use any native dependencies right inside our project.
One way is to add C files right into our project. In the Chapter19 folder in the repository, you will find an example. I have added a file called Test.c with the following content:
int fact(int n)
{
if (n == 0) return 1;
return n * fact(n - 1);
}
This C function calculates a factorial, meaning it multiplies a number by all the numbers below it (for example, 5 becomes 5 × 4 × 3 × 2 × 1). It uses recursion, calling itself with a smaller number until it reaches 0.
In the project file, I have added a reference to that file:
<ItemGroup>
<NativeFileReference Include="Test.c" />
</ItemGroup>
In Home.razor, I have added the following code:
@page "/"
@using System.Runtime.InteropServices
<PageTitle>Native C</PageTitle...