C#执行字符串中的代码总是会出错
我正试图建立一个像这样的指挥系统 每个命令都有一个字符串形式的操作,如下所示:C#执行字符串中的代码总是会出错,c#,C#,我正试图建立一个像这样的指挥系统 每个命令都有一个字符串形式的操作,如下所示: CommandReader.commands.Add(new CommandReader("MESSAGE", @" using System; namespace HelloWorld { /// <summary> /// Summary description for Class1. /// </summary> class H
CommandReader.commands.Add(new CommandReader("MESSAGE", @"
using System;
namespace HelloWorld
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class HelloWorldClass
{
static void Main(string[] args)
{
Console.WriteLine("+"Hello World!"+@");
Console.ReadLine();
}
}
}
"));
我总是在编译器结果中出错
谁能告诉我我的代码有什么问题吗替换
Console.WriteLine(“+”Hello World!”++”)代码>与控制台.WriteLine(““你好,世界!”)代码><代码>“
转义”
由于串接字符串的方式不正确,您的源代码无法编译。应该是这样的:
string action = @"
using System;
namespace HelloWorld
{
class HelloWorldClass
{
static void Main(string[] args)
{
//pay attention to the @ next to hello world
Console.WriteLine(""" + @"Hello World!""" + @");
Console.ReadLine();
}
}
}
";
那么编译器结果上的错误是什么呢?您是否缺少程序集引用,是否忘记了某个地方的;
?查看您的代码控制台。WriteLine(“+”Hello World!”++”);
将显示为控制台。WriteLine(Hello World!);
在Hello World周围缺少引号“
。将其更改为Console.WriteLine(“+”\“你好,世界!”\“+”)代码>Console.WriteLine(“+”你好,世界!”+@”)代码>看起来不对劲…谢谢你@bill这就是问题所在Thank bro:DNo问题,当你在引用文本中使用@“string”
时,事情会变得混乱。我以为是\“
来逃避”
?@MichaelMcGriff是的,在Emmanuel写这篇文章之前,我在上面的评论中给出了正确的答案谢谢,这就是问题所在:D thaaanks!难道不能将整个连接简化为:(\“Hello World!\”)
?为什么在这里使用字符串连接和@
?@RonBeyer是的,当然,我只是不知道连接的目的是什么。。。
string action = @"
using System;
namespace HelloWorld
{
class HelloWorldClass
{
static void Main(string[] args)
{
//pay attention to the @ next to hello world
Console.WriteLine(""" + @"Hello World!""" + @");
Console.ReadLine();
}
}
}
";