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’.
Here’s what seems to be the only similar, but not exactly the same, error available in VS.2008:
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
there should be