Is there eval() for C#?

I’m making a calculator and I use UnityScript already but I’m just curious if eval() is in C#

I think this would also help others that want to use eval()

If there is a way around every scenario that uses eval, here’s how my calculator would work:

User types in equation and its received as a string,

or buttons that would add something to a large string, which adds to the equation

And then I eval the string

Is there another way to do this?

Even so, is there an eval in C#? It would help other possible scenarios.

The short answer is: No, there is no eval function implemented in C#. However it’s possible to achieve something similar.

A few more details:

Using eval is in general a great security risk, especially when the eval string is created by the user. He can literally execute any code within your game / application.

It is highly recommended to never use eval at all if possible. That’s also true for (web) Javascript code.

If you just want to evaluate maths expressions you can use a simple expression parser like the one i’ve written which i’ve posted on the wiki over here. It’s written 100% in managed code and hasn’t any special dependencies. I’ve posted a more detailed answer over here where i included a webplayer example. The parser itself can be extended with custom functions / constants / values with just a single line.

There is no eval on c#. There are multiple reasons (like the compiler is not present on the client etc) but the biggest one is that eval is not cross platform usable as all Ahead of Time (AoT) compilation platforms (iOS, consoles) do explicitely not allow generation of code at runtime for security reasons so eval will actually fail on these platforms.

Bunny83 solution is one that works if its just about maths, if you want to do more complicated things the common solution is having a backend with Node / PHP or something else thats dynamic and send a string there to evaluate, getting back the desired result.