4

When compiling some C# code, I get the error:

A new expression requires () or [] after type

My code is as follows:

request.AddExtension(new ClaimsRequest {
        Country = DemandLevel.Request,
        Email = DemandLevel.Request,
        Gender = DemandLevel.Require,
        PostalCode = DemandLevel.Require,
        TimeZone = DemandLevel.Require,
});

I am working with ASP.NET 2.0.

Can you help explain why this error occurs?

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
Adeel Aslam
  • 1,285
  • 10
  • 36
  • 69
  • You're using .NET 2.0, so you can't do the object initializer. Side note, if you were using .NET 3.5, you can get away without providing the () ony if the class being constructed has a parameterless constructor. – Stealth Rabbi Dec 15 '11 at 13:44
  • 2
    @StealthRabbi that's not quite true. You can use object initializer in .NET 2.0 if you use C# 3.0 (i.e. Visual Studio 2008). – Ray Dec 15 '11 at 13:44
  • @StealthRabbi minor note; object initializers are a *compiler* feature; you can still use C# 3.0 when targeting .NET 2.0; the problem *here*, though, is that ASP.NET 2.0 is involved (presumably configured in pure 2.0 mode), and is presumably performing dynamic compilation with the 2.0 compiler. – Marc Gravell Dec 15 '11 at 13:45
  • Note: if you have .NET 3.5 or higher on the server, you can probably *enable* the 3.0 compiler - see: http://www.hanselman.com/blog/HowToSetAnIISApplicationOrAppPoolToUseASPNET35RatherThan20.aspx – Marc Gravell Dec 15 '11 at 13:48
  • Ok so compiler features can get compiled down to the target .NET framework specified. Does that hold true for .NET 4, since it's not based on 3.5 as 3.5/3.0 is based on 2? Thanks. – Stealth Rabbi Dec 15 '11 at 13:48
  • @StealthRabbi some; not all; `dynamic` needs runtime support, for example; but named arguments and optional parameters should work fine. Variance should be OK, but the inbuilt types (`IEnumerable`, `Action<...>`, `Func<...>`) won't be decorated with variance modifiers. – Marc Gravell Dec 15 '11 at 13:54
  • closely related: http://stackoverflow.com/questions/2667446/object-initializers-not-working-in-listt – nawfal Dec 09 '12 at 00:08

3 Answers3

6

You cannot use object initializers (new T { Property = value }) unless you are writing for C# 3.0 or above.

Unfortunately, for pre-C# 3.0, you'll need to do:

ClaimsRequest cr = new ClaimsRequest();
cr.Country = DemandLevel.Request;
cr.Email = DemandLevel.Request;
cr.Gender = DemandLevel.Require;
cr.PostalCode = DemandLevel.Require;
cr.TimeZone = DemandLevel.Require;
request.AddExtension(cr);

A bit more about object initializers here.

The easiest way to tell what version of C# you are using is by looking at what version of Visual Studio you are using. C# 3.0 came bundled with Visual Studio 2008.

You do have a "way out" however. Prior to .NET 4.0 but after .NET 2.0, all new language and framework features were actually just managed libraries that sat on top of version 2.0 of the CLR. This means that if you download the C# 3.0+ compiler (as part of a later framework), you can compile your code against that compiler. (This is not trivial to do in an ASP.NET environment.)

David Pfeffer
  • 38,869
  • 30
  • 127
  • 202
  • @user1099825: Use the code snip I provided to make this work for pre-C# 3.0. – David Pfeffer Dec 15 '11 at 13:50
  • 2
    @user1099825 VS2005 is many versions behind now; there's a clue in the name - it is (give or take a few days) 2012 now, and software moves *fast*.... just sayin' – Marc Gravell Dec 15 '11 at 13:51
4

Did you perhaps copy that code from another source? It looks like you are trying to use a C# 3.0 (or above) sample (with an "object initializer") in C# 2.0.

In C# 2.0 and below you need:

ClaimsRequest req = new ClaimsRequest();
req.Country = DemandLevel.Request;
req.Email = DemandLevel.Request;
req.Gender = DemandLevel.Require;
req.PostalCode = DemandLevel.Require;
req.TimeZone = DemandLevel.Require;
request.AddExtension(req);
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
-2

just do what it says

request.AddExtension(new ClaimsRequest() {

if you have the new keyword you need to run the constructor ().

Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84