问题描述
大家好,
谁能解释为什么以下代码无法编译?
Hi Everyone,
Can any one explain why the following code doesn''t compile?
int a = 7;
int b = 5;
dynamic oa = a;
dynamic ob = b;
int c = oa - ob;
在此先感谢您.
Thanks in advance.
推荐答案
using System;
namespace ConsoleTester
{
class Program
{
static void Main(string[] args)
{
int a = 7;
int b = 5;
dynamic oa = a;
dynamic ob = b;
int c = oa - ob;
Console.WriteLine(c);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SimpleTestCS
{
class Program
{
static void Main(string[] args)
{
int a = 7;
int b = 5;
dynamic oa = a;
dynamic ob = b;
int c = oa - ob;
Console.WriteLine(c);
}
}
}
因此,打开您的项目属性,并检查您是否正在为.Net 4运行时进行构建
问候
Espen Harlinn
So open your project properties and check that you are building for the .Net 4 runtime
Regards
Espen Harlinn
int a = 7;
int b = 5;
dynamic oa = (dynamic)a;
dynamic ob = (dynamic)b;
int c = oa - ob;
如果需要,请在此处查看.< ^ ]更加清晰.
更新:
1.如OriginalGriff所说,如果您使用.NET Framework 4.0,您的代码将可以正常工作
2.基于错误,显然动态不被视为关键字,因此错误
3.查找缺少的参考.可能您未使用.NET Framework 4.0,在此它是动态关键字.
If needed, look here[^] for more clarity.
UPDATE:
1. As already said by OriginalGriff, your code would work fine if you have .NET framework 4.0
2. Based on the error, it''s clear dynamic is not being considered as a keyword and hence an error
3. Look for the missing reference. Probably you are not using .NET framework 4.0, which would be needed here for dynamic keyword.
这篇关于为什么这段代码无法编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!