Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#将字符串加号转换为实数加号_C#_String - Fatal编程技术网

C#将字符串加号转换为实数加号

C#将字符串加号转换为实数加号,c#,string,C#,String,我想知道是否有办法将字符串+转换为真正的加号。 范例 我想达到,4+2=6 谢谢 您可以编译表达式并在运行时对其求值。看看这里:试试这个 using System.Data; //refference for DataTable string bla = "4+2"; object value = new DataTable().Compute(bla , null); string res=value.ToString();//res=6 尝试以下代码 string bla = "4+2

我想知道是否有办法将字符串+转换为真正的加号。 范例

我想达到,
4+2=6


谢谢

您可以编译表达式并在运行时对其求值。看看这里:

试试这个

using System.Data; //refference for DataTable 

string bla = "4+2";
object value = new DataTable().Compute(bla , null);
string res=value.ToString();//res=6

尝试以下代码

string bla = "4+2";
Expression exp = new Expression(bla);
Console.write(exp.Evaluate());

当然有一个方法-你认为编译器是做什么的?
using System.Data; //refference for DataTable 

string bla = "4+2";
object value = new DataTable().Compute(bla , null);
string res=value.ToString();//res=6
string bla = "4+2";
Expression exp = new Expression(bla);
Console.write(exp.Evaluate());