views:

181

answers:

5

Why I can use Object Initializers in Visual Studio 2008 Windows projects, etc targeted to .NET 2.0 but cannot - in ASP.NET projects targeted to .NET 2.0 ?

I understand that this is C# 3.0 features, but don't - why this possible to use in .NET 2.0 projects.

+6  A: 

Probably because the ASP.Net stuff targeting the 2.0 framework assumes it will run in a mode where it may have to compile some code on the fly. Since it is running in 2.0 mode it will get the 2.0 compiler at that stage (thus anything relying on the 3.0 compiler will fail)

When targeting the 2.0 codebase from a 'normal' project the compilation is done then and there so relying on 3.0 compiler functionality is fine.

ShuggyCoUk
+1  A: 

When you target the .NET 2.0 runtime, you are also targeting the C# 2.0 compiler. That version of the compiler does not understand the 3.0 features.

JonStonecash
A: 

Here's an existing question that deals with using C# 3.0 features in .NET 2.0 projects. It explains which C# 3.0 features are available, which aren't and how to use them.

In order to help you with any specific difficulty, we'll need specific details.

Vojislav Stojkovic
A: 

The best source of information on advanced featured in C# and how they have evolved from C# 1.0 through 3.0 is Jon Skeet's book. He is here on SO quite often and may even offer some additional insight. However, you really should have the book.

Mark Brittingham
A: 

A normal ASP.NET Website compiles on the fly, which means it will use the compiler available to that virtual directory on your web server. If it's set to ASP.NET 2.0, it will use the 2.0 compiler (not the 3.0 compiler targeted to .NET 2.0). I assume when you convert your website to a web application you WILL be able to target 3.0 and still use some of the 3.0 features. I suggest trying that, I would, but I don't have the time at the moment. Let us know if it works ;-)

Erik van Brakel