1

value = 14000;

ternary string expression is "value > 20000 ? 200 : value > 15000 && value < 19999 ? 150 : 100 "

Ans should be 100

Is there any ways to evaluate expression?

Gautam Parmar
  • 686
  • 8
  • 18
  • 1
    For the record, that expression is going to evaluate to 100 if `value` is 19999 or 20000 but I suspect that it should be 150. The `value < 19999` part should probably be `value <= 20000` but, in that case, it's redundant anyway and the whole expression should be `value > 20000 ? 200 : value > 15000 ? 150 : 100`. You already know that `value` is less than or equal to 20000 if you get to the second comparison. – jmcilhinney May 18 '20 at 05:00

1 Answers1

3

You could use Scripting APIs to evaluate the expressions.

The scripting APIs enable .NET applications to instatiate a C# engine and execute code snippets against host-supplied objects and would require Microsoft.CodeAnalysis.CSharp.Scripting package.

For example,

var globals = new Global();
var str = "value > 20000 ? 200 : value > 15000 && value < 19999 ? 150 : 100 ";
var result = await CSharpScript.EvaluateAsync(str,globals:globals);
Console.WriteLine(result);

Where Global is defined as

public class Global
{
    public int @value = 14000;
}

Working Demo

Anu Viswan
  • 17,797
  • 2
  • 22
  • 51