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

The following code for the employee causes the following error when trying to compile it using VS 2008:

Mistake:

The new expression requires () or [] after type

code:

MyClass structure:

public class MyClass
{
    public MyClass() {}

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

Source code example:

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

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

He "works on my machine", but not him. Any idea why?

UPDATE
It targets the 3.5.NET framework.
It uses the System.Collections.Generics namespace.
The MyClass object has a constructor

UPDATE 1:
@ Funky81 - Your example and my example were compiled on my PC.

Update 2:
Enabled MyClass Schema in Sample

UPDATE 3:
@DK - I had my employee adding 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>

: 'providerOption'.

+5
8

, , , VS.2008:

CS1526: (), [] {}

{} , # 3.0. , .

, - .

: ASP.Net. .config,

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

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

.

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

, MyClass .

+4

.NET 3.5? , x.Add( MyClass, .

+3

, ?

, , , MyClass. , , . , , .

, ? .

+2

.NET? , RTF.NET 3.5, .NET 3.5 SP1. .NET 3.5 SP1 ( .NET 2.0 2), System.Web.Caching.Cache.Insert, dev, :/p >

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

, , ...

: " () [] " , ". , a() :

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

- ?

. :

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 - ok
VS2008 SP1 Targetting 3.0 - ok
VS2008 SP1 Targetting 2.0 - ok

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

VS2005 - 8.0.50727.867 ( VS2008/3.5 SP1) - :

() [] 'Test.MyClass.Property1.get' , 'Test.MyClass.Property1.set' , 'Test.MyClass.Property2.get' , 'Test.MyClass.Property2.set' ,

VS2005 - 8.0.50727.762 ( VS2008/3.5 RTM) - :

() [] 'Test.MyClass.Property1.get' , 'Test.MyClass.Property1.set' , 'Test.MyClass.Property2.get' , 'Test.MyClass.Property2.set' ,

, , , . - ?


+2
source

Your code works flawlessly for me. Are you sure that the project compiles to 3.5? Select the project properties from the Project menu, go to the Application tab and see the Target Structure drop-down menu.

+1
source

Does he use System.Collections.Generic?

0
source

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

0
source

All Articles