为什么这段代码无法编译? [英] why this code doesn't compile?

查看:86
本文介绍了为什么这段代码无法编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

谁能解释为什么以下代码无法编译?

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.

推荐答案

您是否正在使用VS2010?如果没有,则动态不可用(它已在V4.0中添加)

否则,您会得到什么错误-它可以编译干净并为我工作...
使用VS2010.
获取相同的编译错误:意外字符''-''
无效的表达方式"
;预期
;预期
"

您是否将代码包含在方法中?
Are you using VS2010? If not, then dynamic is not available (it was added at V4.0)

Otherwise, what error do you get - it compiles clean and works for me...
"am using VS2010.
Getting same compilation Errors: Unexpected character''-''
Invalid expression term "
; expected
; expected
"

Have you included your code inside a method?
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);
            }
        }
    }


OriginalGriff是正确的,它可以很好地编译和运行:
OriginalGriff is right, this compiles and runs nicely:
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


您需要在此处显式类型转换.

试试:
You need explicit type conversion here.

Try:
int a = 7;    
int b = 5;        
dynamic oa = (dynamic)a;    
dynamic ob = (dynamic)b;       
int c = oa - ob;



如果需要,请在此处查看.<



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.


这篇关于为什么这段代码无法编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆