In C# there is no such function as eval. But i will show you a simple trick how to make code that works like eval function. The trick is to create in memory a C# code then compile it and use as evaluator. If you want to use a same eval many times it is recommended to compile it just once and then just call the assembly. That is important because compilation may take a long time.

Ok here is a code in with we can inject some custom code in a runtime.

private static object compiled = null;

static void Main(string[] args)
    Console.Write("Enter some C# code. (Eg. Math.Abs(x)+1): ");
    string code = Console.ReadLine();
    Console.Write("Enter a double value: ");
    double value = Convert.ToDouble(Console.ReadLine());

    double result = ComputeResult(value);
    Console.WriteLine("Result = {0}", result);

public static void Initialize(string function)
    ICodeCompiler compiler = (new CSharpCodeProvider().CreateCompiler());
    CompilerParameters compilerParameters = new CompilerParameters();
    compilerParameters.GenerateExecutable = false;
    compilerParameters.GenerateInMemory = true;

    StringBuilder code = new StringBuilder();
    code.Append("using System; \n");
    code.Append("namespace Eval { \n");
    code.Append("  public class Evaluator { \n");
    code.Append("       public double Function(double x) {\n");
    code.Append("           return " + function + ";\n");
    code.Append("       }\n");
    code.Append("   }\n");

    CompilerResults cr = compiler.CompileAssemblyFromSource(compilerParameters, code.ToString());
    Assembly assembly = cr.CompiledAssembly;
    compiled = assembly.CreateInstance("Eval.Evaluator");

public static double ComputeResult(double x)
    if (compiled == null)
        throw new NullReferenceException("To use this method you must initialize Eval by its paramether");
    MethodInfo mi = compiled.GetType().GetMethod("Function");
    return (double)mi.Invoke(compiled, new object[] { x });
Enter some C# code. (Eg. Math.Abs(x)+1): Math.Abs(x)+1
Enter a double value: -12345

Result = 12346
Enter some C# code. (Eg. Math.Abs(x)+1): x*x+Math.Sin(x)*10
Enter a double value: 3,1415

Result = 9,86994878589661
Enter some C# code. (Eg. Math.Abs(x)+1): Math.Pow(x,3)-Math.Pow(x,2)+x+1
Enter a double value: 2

Result = 7

Disadvantage of this method is that the CSharpCodeProvider().CreateCompiler() is obsolete but anyway it works well. Another way to make eval is by using a CodeDom.

Eval Console Application