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

The following code for a co-worker throws the following error when he tries to compile it using 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

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

Update 2:

Included schema of MyClass in sample

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

<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’.

2

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

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

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.

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

configurationsystem.codedomcompilerscompiler @language="c#..."

there should be

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

1

It gives me bad compile message in my PC

Try this

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

Notice, there is another bracket after MyClass class creation.

2

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.

0

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.

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

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:

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:

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 RTM – Targetting 3.5 – Compiles ok
VS2008 RTM – Targetting 3.0 – Compiles ok
VS2008 RTM – Targetting 2.0 – Compiles ok

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

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

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

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

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

2

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.

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật