类型编译错误后的新表达式需要 () 或 [] - C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/389644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:37:27  来源:igfitidea点击:

A new expression requires () or [] after type compilation error - C#

c#visual-studio-2008.net-3.5

提问by Michael Kniskern

The following code for a co-worker throws the following error when he tries to compile it using VS 2008:

同事的以下代码在尝试使用 VS 2008 编译时抛出以下错误:

Error:

错误:

A new expression requires () or [] after type

新表达式在类型后需要 () 或 []

Code:

代码:

MyClass Structure:

我的课堂结构:

public class MyClass
{
    public MyClass() {}

    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

Sample Source Code:

示例源代码:

List<MyClass> x = new List<MyClass>();

x.Add(new MyClass 
{
    Property1 = "MyValue",
    Property2 = "Another Value"
});

It "works on my machine", but not his. Any idea why?

它“适用于我的机器”,但不适用于他的。知道为什么吗?

UPDATE
He is targeting the 3.5 .NET framework
He is using the System.Collections.Generics namespace
The MyClass object does have a constructor

更新
他的目标是 3.5 .NET 框架
他正在使用 System.Collections.Generics 命名空间
MyClass 对象确实有一个构造函数

UPDATE 1:
@Funky81 - Your example and my example were able to compile on my PC.

更新 1:
@Funky81 - 您的示例和我的示例能够在我的 PC 上编译。

Update 2:
Included schema of MyClass in sample

更新 2:
在示例中包含 MyClass 的架构

UPDATE 3:
@DK - I had my co-worker add the following configuration section to his application:

更新 3:
@DK - 我让我的同事将以下配置部分添加到他的应用程序中:

<system.codedom>
        <compilers>
            <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5"/>
                <providerOption name="WarnAsError" value="false"/>
            </compiler>
            <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <providerOption name="CompilerVersion" value="v3.5"/>
                <providerOption name="OptionInfer" value="true"/>
                <providerOption name="WarnAsError" value="false"/>
            </compiler>
        </compilers>
    </system.codedom>

And he received the following compilation error: Unrecognized element 'providerOption'.

他收到以下编译错误:无法识别的元素“providerOption”。

采纳答案by DK.

Here's what seems to be the only similar, but not exactly the same, error available in VS.2008:

以下是 VS.2008 中似乎唯一相似但不完全相同的错误:

Compiler Error CS1526 : A new expression requires (), [], or {}after type

编译器错误 CS1526:新表达式 在类型后需要 ()、[]或 {}

Note those {}in error message, which are part of c# 3.0 syntax. This is not related to framework version, but to the version of the language.

请注意{}错误消息中的内容,它们是 c# 3.0 语法的一部分。这与框架版本无关,而与语言版本有关。

My bet is that somehow a different version of compiler was used.

我敢打赌,不知何故使用了不同版本的编译器。

Added: this looks like a likely issue with ASP.Net. Place to check is in .config file(s), element

补充:这看起来像是 ASP.Net 的一个可能问题。检查位置在 .config 文件中,元素

configuration\system.codedom\compilers\compiler @language="c#..."

there should be

应该有

<providerOption name="CompilerVersion" value="v3.5"/>

回答by John Sheehan

Is his project targetting .NET 3.5? If not, that error would be thrown on the x.Add(new MyClass line because the new class doesn't have a constructor or indexer specified.

他的项目是针对 .NET 3.5 的吗?如果没有,该错误将在 x.Add(new MyClass 行上抛出,因为新类没有指定构造函数或索引器。

回答by P Daddy

Your code works flawlessly for me. Are you sure the project is being compiled to the 3.5 framework? Select the project properties from the Project menu, go to the Application tab, and look at the Target Framework dropdown.

你的代码对我来说完美无缺。您确定该项目正在编译为 3.5 框架吗?从 Project 菜单中选择项目属性,转到 Application 选项卡,然后查看 Target Framework 下拉列表。

回答by Funky81

It gives me bad compile message in my PC

它在我的 PC 中给了我错误的编译消息

Try this

尝试这个

x.Add(new MyClass()
{
    Property1 = "MyValue",
    Property2 = "Another Value"
});

Notice, there is another bracket after MyClass class creation.

请注意,在 MyClass 类创建之后还有另一个括号。

回答by Jay Bazuzi

Are you sure you're using the exact same code?

您确定您使用的是完全相同的代码吗?

The code you supplied doesn't compile, because it lacks a declaration of MyClass. Please supply us with a full compiland, not just a code snippet. Then send that file to your friend to make sure you still see the different behavior on your machine.

您提供的代码无法编译,因为它缺少MyClass. 请向我们提供完整的编译器,而不仅仅是代码片段。然后将该文件发送给您的朋友,以确保您仍能在您的机器上看到不同的行为。

The error message points to a line & column, right? Tell us where.

错误消息指向一行和列,对吗?告诉我们在哪里。

回答by Kev

Are you both at the same .NET service pack level? I got bitten today because one machine was .NET 3.5 RTM, the other was .NET 3.5 SP1. In .NET 3.5 SP1 (which also installs .NET 2.0 SP2), they introduced a new overload to the System.Web.Caching.Cache.Insert method which I used on my dev box and thenfailed on an intermediate pre-build environment:

你们都处于相同的 .NET 服务包级别吗?我今天被咬了,因为一台机器是 .NET 3.5 RTM,另一台是 .NET 3.5 SP1。在 .NET 3.5 SP1(也安装了 .NET 2.0 SP2)中,他们为 System.Web.Caching.Cache.Insert 方法引入了一个新的重载,我在我的开发箱上使用了该方法,然后在中间预构建环境中失败:

public void Insert(
    string key,
    Object value,
    CacheDependency dependencies,
    DateTime absoluteExpiration,
    TimeSpan slidingExpiration,
    CacheItemUpdateCallback onUpdateCallback
)

Took me a few mins to workout why this was broken on one machine but not the other...

我花了几分钟来弄清楚为什么这在一台机器上坏了,而在另一台机器上没有……

Update:the error message 'A new expression requires () or [] after type' often means you've missed a constructor parens. Are you sure you haven't missed a () off of the line:

更新:错误消息“A new expression requires () or [] after type”通常意味着您错过了构造函数括号。你确定你没有错过 () 下线:

List<MyClass> x = new List<MyClass>();

Or somewhere near by?

或者附近的什么地方?

Update Again: I have built the following:

再次更新:我已经构建了以下内容:

using System;
using System.Collections.Generic;

namespace Test
{
    public class MyClass
    {
        public MyClass() { }

        public string Property1 { get; set; }
        public string Property2 { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<MyClass> x = new List<MyClass>();
            x.Add(new MyClass
            {
                Property1 = "Kev",
                Property2 = "Kev 2"
            });
        }
    }
}

VS2008 SP1 Targetting 3.5 - Compiles ok
VS2008 SP1 Targetting 3.0 - Compiles ok
VS2008 SP1 Targetting 2.0 - Compiles ok

VS2008 SP1 目标 3.5 - 编译正常
VS2008 SP1 目标 3.0 - 编译正常
VS2008 SP1 目标 2.0 - 编译正常

VS2008 RTM - Targetting 3.5 - Compiles ok
VS2008 RTM - Targetting 3.0 - Compiles ok
VS2008 RTM - Targetting 2.0 - Compiles ok

VS2008 RTM - 目标 3.5 - 编译正常
VS2008 RTM - 目标 3.0 - 编译正常
VS2008 RTM - 目标 2.0 - 编译正常

VS2005 - 8.0.50727.867 (on machine with VS2008/3.5 SP1) - Fails with:

VS2005 - 8.0.50727.867(在装有 VS2008/3.5 SP1 的机器上) - 失败:

A new expression requires () or [] after type 'Test.MyClass.Property1.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property1.set' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.set' must declare a body because it is not marked abstract or extern

类型 'Test.MyClass.Property1.get' 之后的 new 表达式需要 () 或 [] 必须声明一个主体,因为它没有被标记为 abstract 或 extern
'Test.MyClass.Property1.set' 必须声明一个主体,因为它没有被标记abstract 或 extern
'Test.MyClass.Property2.get' 必须声明一个主体,因为它没有标记为 abstract 或 extern
'Test.MyClass.Property2.set' 必须声明一个主体,因为它没有标记为 abstract 或 extern

VS2005 - 8.0.50727.762 (on machine with VS2008/3.5 RTM) - Fails with:

VS2005 - 8.0.50727.762(在装有 VS2008/3.5 RTM 的机器上) - 失败:

A new expression requires () or [] after type 'Test.MyClass.Property1.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property1.set' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.get' must declare a body because it is not marked abstract or extern
'Test.MyClass.Property2.set' must declare a body because it is not marked abstract or extern

类型 'Test.MyClass.Property1.get' 之后的 new 表达式需要 () 或 [] 必须声明一个主体,因为它没有被标记为 abstract 或 extern
'Test.MyClass.Property1.set' 必须声明一个主体,因为它没有被标记abstract 或 extern
'Test.MyClass.Property2.get' 必须声明一个主体,因为它没有标记为 abstract 或 extern
'Test.MyClass.Property2.set' 必须声明一个主体,因为它没有标记为 abstract 或 extern

I am inclined to think the problem is not with the code you are presenting unless a screen shot is supplied with an example of the compile error using the above code or similar. Any chance of boiling things down to something simple to eliminate red herrings?

我倾向于认为问题不在于您提供的代码,除非使用上述代码或类似代码提供了带有编译错误示例的屏幕截图。有没有机会把事情归结为简单的事情来消除红鲱鱼?

Cheers
Kev

干杯
凯夫

回答by Neil Barnwell

Add "()" after new MyClass in the code example, or remove the empty constructor from MyClass.

在代码示例中在 new MyClass 后添加“()”,或从 MyClass 中删除空构造函数。