7

I'm just starting to learn C# and its relationship to .NET. If say I wanted to take advantage of the latest C# language conventions, but wanted to target, say a .NET 2.0 framework, could I do that? Or does using the latest C# mean I have to use the latest .NET?

tralston
  • 2,825
  • 3
  • 19
  • 22
  • @JustinHarvey, Does it mean you can't target C# 6.0 code to .NET 4.5 CLR? Any references to this claim? As far as I hear, most of C# 6 advantages are gained using compiler techniques. – haim770 Feb 23 '15 at 08:48
  • I'll expect the situation for c# 6 to be similar to to c#5: you can compile c# 5 to run on the 2.0 CLR: many language features new to c#5, like type inferrence, were compiler improvements. Some other features, like the use of dynamics or async, DO need the newer CLR of libraries that are not available in the newest version. – realbart Feb 23 '15 at 08:55
  • For those of you wondering, normally I wouldn't need to target something like 2.0, but my project is a tool that can be used on earlier systems like XP, thus it needs to have the broadest audience of .NET profiles. – tralston Feb 23 '15 at 10:13

6 Answers6

7

C# as a language is not dependent on .net framework.

For example: Extension methods is a feature released with C# 3.0 which came along with .Net 2.0. Extension methods depends on ExtensionAttribute which lives in "mscorlib.dll" which was added in .Net 3.5. But you can use Extension methods in .Net 2.0 given that you provide your own ExtensionAttribute in your library itself. It doesn't needs to be in mscorlib. Refer this question for more info.

As we know is new in C# 5.0 which was released with .Net 4.5, but we can use async-await in .Net 4.0 itself.

Sameway, most of the dependencies of language features can be defined in your own assembly to make it work. So it doesn't need the particular .Net framework version.

Community
  • 1
  • 1
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • 2
    If you look at the .NET framework as a combination of "CLR + Managed libraries", it's easier to explain why new features (such as async/await) come out with new framework versions. – Yuval Itzchakov Feb 23 '15 at 09:00
  • So does that mean that any new feature can be back-ported into .NET 2.0, assuming the proper libraries are referenced? I'm assuming that the version of Visual Studio does matter in some cases, where it recognizes newer language conventions (e.g. auto-property default values). – tralston Feb 23 '15 at 10:02
  • @tralston Yes almost anything can be done with .Net 2.0 given that no CLR support is required for those features. New visual studio support is required to understand new keywords and features but you can work without visual studio (just with command line compiler) if you have patience :) – Sriram Sakthivel Feb 23 '15 at 10:08
  • @SriramSakthivel What direction would you point me in if I was trying to understand how a particular feature was implemented? Say I wanted to use a newer keyword or syntax, where would I start looking? – tralston Feb 23 '15 at 10:11
  • 1
    @tralston I extensively use decompiler(IlSpy, Reflector) to find the implementation. Now you can find .Net [source here](http://referencesource.microsoft.com/). Compiler source is [here](https://github.com/dotnet/roslyn). You can find blogs about all the topics which you're interested in. Many of them talks about the internals of the feature. – Sriram Sakthivel Feb 23 '15 at 10:18
0

you can use features of lastest visual studio editions (2013, 2015), but the code must be according to 2.0 specifications to allow a .NET 2.0 compilation (older versions)

0

Yes, C# and .Net versions are dependent. According to my understanding C# is a programming language which works using .Net Technology.

Learning C# means learning the basic syntax, which is almost the same in all .Net versions. So for learn c#, it doesn't matter which .Net version you must target. Different .Net versions adds up certain new features or functionality. Any way its better to understand latest .Net version which will help you in projects.

0

You can use C# features from earlier versions with later versions of .Net, for example, you can use any C# 2.0 feature with .Net 3.5, this does not work the other way arround, so you cannot use .Net 3.5 features with the c# 2.0 compiler.

For more details see: https://msdn.microsoft.com/en-us/library/ff602939(v=vs.110).aspx

0

C# 6.0 is available in .NET 5.0. It depends on the CLR. C# 4, 4.5 and 4.5.1 are using the CLR Version 4.

Trx
  • 31
  • 3
0

Yes C# is dependent on .net versions. The dependence between the both is on features and technologies provided. Refer following link:

https://msdn.microsoft.com/en-us/library/bb822049%28v=vs.110%29.aspx