Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 4.0 and .Net 3.5

Tags:

c#

.net

.net-3.5

So we've finally got VS2010 on some developer stations at work and can use the C# 4.0 features. Although most of what we develop will still have to target .Net 3.5 for the time being.

When I start a new project and set the target to .Net 3.5, it still allows me to use C# 4.0 such as dynamic. Can you therefore use C#4.0 features whilst targetting .net 3.5 and will these features work in environments where .Net 4.0 is not available?

Thanks.

like image 766
Darren Young Avatar asked Jun 02 '11 13:06

Darren Young


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


1 Answers

dynamic code will not compile if you target the .NET 3.5 framework.

To be more clear, the compiler will allow you to define and assign a dynamic variable, such as:

dynamic x = 3; 

That one line of code will compile, because dynamic just compiles to object as far as types are concerned. But if you then try to do anything with that variable, as in:

Console.WriteLine(x); 

... then the compiler would have to generate code to discover/coerce the real type, which it cannot do; you'll get the following compile errors:

  1. Predefined type 'Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
  2. One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?

The C# 4 compiler relies on the DLR and specifically the Microsoft.CSharp assembly for everything related to dynamic. These aren't available in .NET 3.5. So the answer is no, you cannot use dynamic when targeting Framework version 3.5.

like image 141
Aaronaught Avatar answered Sep 20 '22 16:09

Aaronaught