DEV Community

anonymous675
anonymous675

Posted on

Compile C# to WebAssembly?

How can I compile C# to WebAssembly (simple functions to calculate things) and run it in the browser?

Top comments (2)

Collapse
 
devarjunan profile image
dev-arjunan

Hi, Take a look at below article for more information.

codewithmukesh.com/blog/getting-st...

Collapse
 
anonymous675 profile image
anonymous675 • Edited

But how Blazor compile c# to WebAssembly? I want to take c# code that calculates things (Temperature, Weight) and compile it into WebAssembly, Then use that code with Node.js and Javascript app. See Wikipedia example: en.wikipedia.org/wiki/WebAssembly

code in C:

int factorial(int n) {
  if (n == 0)
    return 1;
  else
    return n * factorial(n-1);
}
Enter fullscreen mode Exit fullscreen mode

Same in WebAssembly:

(func (param i64) (result i64)
  local.get 0
  i64.eqz
  if (result i64)
      i64.const 1
  else
      local.get 0
      local.get 0
      i64.const 1
      i64.sub
      call 0
      i64.mul
  end)
Enter fullscreen mode Exit fullscreen mode