Using Latest C# Features in Any Old .NET Version

  Рет қаралды 31,774

Nick Chapsas

Nick Chapsas

Жыл бұрын

Check out my courses: dometrain.com
Become a Patreon and get source code access: / nickchapsas
Join the NDC Conferences Giveaway: mailchi.mp/nickchapsas/ndc
Hello everybody I'm Nick and in this video I will show you how you can use the latest C# 11 in your old codebases such as .NET Framework or UWP.
Give PolySharp a star on GitHub: github.com/Sergio0694/PolySharp
Workshops: bit.ly/nickworkshops
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet

Пікірлер: 139
@carldaniel6510
@carldaniel6510 Жыл бұрын
I love this package. Most of the apps I maintain are stuck at framework 4.8 and will likely never move to anything derived from Core, so this is very handy. I'm OK with not having new language features that require CLR changes, but language features that are purely compiler and library changes should be available on all platforms and I will definitely use them. Makes moving back & forth between framework and core so much less painful.
@wojciechwilimowski985
@wojciechwilimowski985 Жыл бұрын
You almost made 1st place on my "top 3 mind blown moments this week", but a few days ago I learned that if you add a Select method to any class, you can use it in LINQ Query Syntax and my head exploded
@novalogic1265
@novalogic1265 Жыл бұрын
Can you send an example for Select?
@maxsteffey6521
@maxsteffey6521 Жыл бұрын
WAIT WHAT example??
@ProtossOP
@ProtossOP Жыл бұрын
We need more info my dude, share the wisdom.
@protox4
@protox4 Жыл бұрын
Nice find. C# has a lot of hidden duck-typed features like that. For example, you can use a foreach loop over an object that doesn't implement IEnumerable, as long as it has a GetEnumerator method that returns a type that has a MoveNext method and Current property. That also works with extensions, so you can add an extension to add foreach support on any type. Same thing with await extensions.
@bWFyY29u
@bWFyY29u Жыл бұрын
.
@MAO-sz3wr
@MAO-sz3wr Жыл бұрын
That's exactly what I needed, without knowing that I needed it. Thanks a lot, Nick!
@zwatotem
@zwatotem Жыл бұрын
How did you get that timing so well? I was just using C#11 in .NETStandard2.0 and got stuck on list patterns. I took a break and here is the exact video I needed. Thank you! (Also shout out to PolySharp)
@Delphi80SVK
@Delphi80SVK Жыл бұрын
Same here, i got task to try to convert our project from NET5 for NETStandard2.0 (for compatibility reasons with older projects) and it fails to compile for using C#11 language features. Just hour ago i started to remove all new stuff i am using from c#11 and back to C#7.3 😀Now i can stay at c#11 in NETStandard 2.0. I will try PolySharp for my situation if it works for me or not.
@Delphi80SVK
@Delphi80SVK Жыл бұрын
Problem here seems to be that when u start using new stuff like ReadOnlySpan (not language stuff) you are out of luck in older projects, as i am now ;-)
@Luke-me9qe
@Luke-me9qe Жыл бұрын
Well, do you know the movie The Truman Show?
@zachemny
@zachemny Жыл бұрын
@@Delphi80SVK For ReadOnlySpan you can use official Microsoft's System.Memory nuget package. It can be used from .NET FW 4.5.
@Brondahl
@Brondahl Жыл бұрын
Ditto. Literally 3 hours ago was talking with a team mate about bumping our C# version up past what is nominally supported!
@cn-ml
@cn-ml Жыл бұрын
This is perfect, exactly what i just needed now. Thank you
@1Gargo
@1Gargo Жыл бұрын
Thank you for this knowledge! I was just wishing the other day to be able to do exactly that. You saved my sanity!
@drm1983
@drm1983 Жыл бұрын
Amazing! just what I was looking for. After working a a project with .Net6 it feels a bit bitter to work in .Net Framework 4.7. This will really help.
@astralpowers
@astralpowers Жыл бұрын
I didn't know about this package. I used another package Nullable package to support nullable annotations, but this goes beyond that. Will be updating my project to use this.
@antonmartyniuk
@antonmartyniuk Жыл бұрын
This library is awesome, I was really handy for me today. Never knew before it
@luc9volts
@luc9volts Жыл бұрын
That depends on the compiler installed on your machine. If you make these changes and a colleague of yours doesn't have the correct compiler, it could be a problem.
@Petoj87
@Petoj87 Жыл бұрын
Thanks for another great video, haven't tried it yet but seems like a life saver for source generators as they are stuck on standard 2.0 if I remember correctly.
@mariocamspam72
@mariocamspam72 Жыл бұрын
Sergio would be proud
@Sergio0694
@Sergio0694 Жыл бұрын
I am 😄
@F1nalspace
@F1nalspace Жыл бұрын
Wow thats new for me, i knew you could change the language version - but i always got that those errors you are talking about. Great that there is a way to solve that ;-) Really thanks for getting that out!
@jongeduard
@jongeduard Жыл бұрын
Thanks a lot for this very practical tip!! Indeed I didn't know about this single and so complete dedicated package. (Still need to support a lot of hybrid old and modern projects at work) I have been using individual packages instead like IndexRange and searched samples of those manual code fixes including that IsExternalInit one. Though it's strange but kind of an exception to the rule seems the System.Memory package from microsoft itself that gives kind of official backwards compatibility support for older versions to be able to use Span and Memory, even though it's not really mentioned in their documentation.
@computer9764
@computer9764 Жыл бұрын
Very cool It's still something I wish was just supported out of the box, though
@furious385
@furious385 Жыл бұрын
As usually interesting and useful content.
@sumittandon6246
@sumittandon6246 Жыл бұрын
Good Recco, Thanks Nick
@davidb.1905
@davidb.1905 Жыл бұрын
Awesome ! Thanks you very much 👍.
@mickaelveril7766
@mickaelveril7766 Жыл бұрын
I use IsExternalInit in order to have a record in MobileDeviceSharp one of my C# library (because I want to be backward compatible to NET Standard 2) but maybe I can switch to this solution ! Thanks you Nick !
@gavinlangley8411
@gavinlangley8411 Жыл бұрын
oo very nice Nick thanks
@quelqunderandom6143
@quelqunderandom6143 Жыл бұрын
5:06 instructions unclear typed source...net, website not found
@nickchapsas
@nickchapsas Жыл бұрын
HAHAHAHAHA
@renetittel17
@renetittel17 Жыл бұрын
Danke!
@metalhead6604
@metalhead6604 Жыл бұрын
Damn that is really cool!
@billy65bob
@billy65bob Жыл бұрын
It's worth bumping the C# version anyway. While there are some that depend on these stubs, and others that require runtime changes, there are also many features that require neither.
@razveck
@razveck Жыл бұрын
This is insane!
@user-pp9sx4ky9w
@user-pp9sx4ky9w Жыл бұрын
The best!
@darioabbece3948
@darioabbece3948 Жыл бұрын
Finally .Net sandbox version is now
@modernkennnern
@modernkennnern Жыл бұрын
I heard about this about a month ago and replaced `IsExternalInit` and `Nullable` on the project I'm working on at work. Finally being able to use required 👏
@ParkImaging
@ParkImaging Жыл бұрын
Very nice!
@kostasgkoutis8534
@kostasgkoutis8534 Жыл бұрын
Nice video. I knew about the first approach you have shown (adding on your own types + namespace) but last time I checked it with Razor views on an old MVC project, records just didn't work. There is some other trick there that you could pull off by introducing some other Nuget package but I cannot remember the name.. Oh well.
@davidwilliss5555
@davidwilliss5555 Жыл бұрын
I gotta try this! I work on a project that's stuck in .NET 4.8 and because we have so much 4.8 code and I would love to use new language features. It seems like Microsoft could just provide a nuget package with all these things.
@soverain
@soverain Жыл бұрын
I did the trick for init only setters but in Unity, which is stuck on a incomplete C# 9 implementation and dotnet 4.x I think.
@EdKolis
@EdKolis Жыл бұрын
This is magic. What next, a Python package that lets you say "from csharp import eleven"? 😛
@tmhchacham
@tmhchacham Жыл бұрын
Nice!
@vm123
@vm123 Жыл бұрын
Can Polysharp enable extended pattern matching for fw472? Range pattern and nested properties?
@inzyster
@inzyster Жыл бұрын
Yes.
@Dimich1993
@Dimich1993 Жыл бұрын
This is cool! Anyway hopefully I won't have to program any .NET Framework code ever again!
@user-ys6nh6fe5o
@user-ys6nh6fe5o Жыл бұрын
holly cow!
@dekryptid
@dekryptid Жыл бұрын
If you're not using Rider, instead using Visual Studio 2022, how can one adjust the language version with the dropdown disabled?
@nickchapsas
@nickchapsas Жыл бұрын
You can use the LangVersion csproj parameter
@nanvlad
@nanvlad Жыл бұрын
Does it work with Mono? Cool feature!
@adrian_franczak
@adrian_franczak Жыл бұрын
Remember to update msbuild in pipelines xD!
@nickchapsas
@nickchapsas Жыл бұрын
Big true!
@bluecup25
@bluecup25 Жыл бұрын
Mind = blown
@marvinbrouwer459
@marvinbrouwer459 Жыл бұрын
I actually did some of this to use C# 10 in a multi-framework sln. And when I was finished I heard of this package 😅 Do you think it makes sense to migrate to this package maybe?
@rhoward99
@rhoward99 Жыл бұрын
This is great information! I noticed that you are using Rider, and it made switching language versions really simple. Can you suggest how to make this same change to the project using VS 2019? EDIT: Never mind. Should have Googled it myself first!
@nickchapsas
@nickchapsas Жыл бұрын
I haven't used VS in more than 4 years but you can always simply set the LangVersion parameter in the csproj
@tcl78
@tcl78 Жыл бұрын
@@nickchapsas out of curiosity, why did you switch to Rider, which you need to pay for via a monthly or yearly subscription, when Visual Studio Community is free and is supported directly by Microsoft?
@nickchapsas
@nickchapsas Жыл бұрын
@@tcl78 Because I make up the money of the Rider subscription by how much better Rider is than VS and how much more productive it makes me
@tcl78
@tcl78 Жыл бұрын
@@nickchapsas interesting, perhaps one day you can make a video about the differences between Visual Studio, VS Code and Rider? Thanks
@vm123
@vm123 Жыл бұрын
It does not work for me at all. Keep throwing Error CS1617 Invalid option '11.0' for /langversion. I’m using latest vs 2022 preview and after adding lang version 11 to csproj file, visual studio keeps throwing Invalid option error. Does it even suppose to work in visual studio? I needed expressionCallerAttribute but due to error above I manually copied the content of generated attribute and added to project. It looks to be working.
@arithex
@arithex Жыл бұрын
It is mystifying to me that MS broke the line of compatibility of C# back to .Net Framework.. I kinda suspected 90% of the recent language innovations were all implemented at compiler-level, not runtime.. but this confirms it actually looks more like 99%.
@ROBdk97
@ROBdk97 Жыл бұрын
How does the performance look here then? Is it worse or the same compared to the .Net version that has official support for this?
@trevorprice2490
@trevorprice2490 Жыл бұрын
Very useful and interesting. Are there any drawbacks or gotchas to doing this?
@strayj2767
@strayj2767 Жыл бұрын
leaving a comment here in case this gets answered. Hopefully there isn't any performance drawbacks or whatnot
@nickchapsas
@nickchapsas Жыл бұрын
The main drawback is that it’s not officially supported by Microsoft
@protox4
@protox4 Жыл бұрын
Possible type clashes if some libraries do the same trick. No performance penalties, it's just metadata.
@palecskoda
@palecskoda Жыл бұрын
I believe there are major drawbacks to using this approach in libraries as it causes the consumers to be unable to use the original types easily even if the consumer uses a .NET version where they are already available.
@lexer_
@lexer_ Жыл бұрын
Is it possible to use Span in async methods in .net 4.8 somehow?
@Sergio0694
@Sergio0694 Жыл бұрын
No, you cannot use Span in any async method anywhere, that's by design. You will have to use Memory in those scenarios, or equivalent types.
@godfreyofbouillon966
@godfreyofbouillon966 Жыл бұрын
I am getting lost between all these .net framework, .net core and .net standard versions. If I start a new desktop gui app project now, that's likely to last and need to be supported / improved for years to come, what's the best .NET to use? Is .Net Framework 4.8 with Win Forms good enough for this purpose or is it gonna be abandoned soon? End user is supposed to run Windows but if I can make it cross platform with little to no additional work, all the better.
@murrty
@murrty Жыл бұрын
I'm pretty sure all C# features are able to be used on Framework since the compiler is shared between Framework and NET, as long as you want to go down the rabbit hole of implementing them into Framework. The only thing keeping me from upgrading to NET 6 is the fact that MainMenu and ContextMenu were removed on WinForms. There was no reason for them to remove both of those controls and I haven't found a way to bring them back. I'm sure it's possible to forward-port them, but I haven't tried since WinForms controls are more intricate than mostly empty classes.
@modernkennnern
@modernkennnern Жыл бұрын
As long as it doesn't require a change to the runtime, yes. Which effectively means every change to C# other than the 2 interface-related ones; Static members and default members
@Sergio0694
@Sergio0694 Жыл бұрын
@@modernkennnern There's actually a few more as well. For instance, covariant return types for interfaces, ref fields, generic attributes and numeric IntPtr/UIntPtr are also not supported. But yeah that's relatively a small part out of all the new features, which will indeed work downlevel too 🙂
@murrty
@murrty Жыл бұрын
​@@Sergio0694 Alongside the static and default interface members (so close, yet so far away), ref fields is added to that list. Generic attributes (I haven't fully tested it, but it compiles with an empty class so, ayo) and numeric pointers do work on framework if you change the langversion in csproj. I would be lost without numeric pointer.
@limbique
@limbique Жыл бұрын
Wasn't able to use this in Visual studio 2019.
@tuberklz
@tuberklz Жыл бұрын
wow
@anilpillay2193
@anilpillay2193 Жыл бұрын
Out of curiosity, on production environment which FW version should we install to run. Should we install .Net FW 3.5 and .Net 7 or just .Net 3.5? My hunch is just .Net FW 3.5
@qj0n
@qj0n Жыл бұрын
your hunch is correct
@gctypo2838
@gctypo2838 7 ай бұрын
Entirely depends on what you're running and how far into development you are. If your code is built on Fw3.5, use Fw3.5. If your code is built on .NET 7, install .NET 7 (though I'd advise instead targeting LTS .NET 6). If you haven't started writing your software yet, target an LTS version on the mainline - you'll save the devs that follow you a lot of headaches and frustration; working with legacy framework versions is pain.
@abhishekbagchi6052
@abhishekbagchi6052 Жыл бұрын
Does anyone know how can change the language version from Visual Studio?
@Masterrunescapeer
@Masterrunescapeer Жыл бұрын
Just edit in the csproj file, 11 # Inside the project tags
@derangedftw
@derangedftw Жыл бұрын
Has anyone released a backported library for this? Should have watched the whole video. Poly#
@oddikaro8236
@oddikaro8236 Жыл бұрын
One question Nick maybe slighlty away from topic: there is an interesting paradox between .NET vs .NET Framework. While .NET is newer, LTS versions have support "only for" 3 years, while .NET Framework, "old and forgotten" has "infinite support" from Microsoft since a big % of important apps were developed in .NET Framework so Microsoft is "forced" to provide eternal support. Can you give your opinion on this?
@goodstas
@goodstas Жыл бұрын
Thank you for your video. I followed it and tried to do the same but it doesn't work for me.. i mean to use PolySharp. The first trick (to add implementation by myself) i already used in the past. I created Console App for .NET Framework 4.7.2 and added to the project file 11.0. I installed the PolySharp package .I don't see that it produces some source generators files and i got the exceptions from the compiler.. will be appreciate for advise what's i could do wrong. I didn't see the Polysharp in my Reference list but there is packages.config file and there is a reference there.
@adrgri
@adrgri Жыл бұрын
You have to migrate your project to PackageReference because for some reason source generators don't want to work with packages.config. Right click on your packages.config and select the migrate option. Keep in mind that only VS 2017 and up and understand PackageReference, so if you're using VS 2015, you're out of luck
@Tsunami14
@Tsunami14 Жыл бұрын
Certainly interesting from an academic perspective, but it still seems like you're playing with fire (even with PolySharp) since you're Frankensteining different compiler/language versions together in a way that's not been battle-tested by Microsoft. If it's a pure syntactic sugar change, and I fully understood what the underlying lowered code would be, then ...maybe?
@theMagos
@theMagos Жыл бұрын
There hasn't been a single patch/update of VS2022 that hasn't introduced a new annoying bug, so "battle-tested by Microsoft" doesn't hold much in my book
@zariumsheridan3488
@zariumsheridan3488 Жыл бұрын
Yeah, I would not want to be the one who added this in a real big project if there is a risk this might be identified as a reason for some high impact heisenbug after a long and expensive investigation...
@Rovsau
@Rovsau Жыл бұрын
Would this work for projects in Unity?
@Max-bx2dc
@Max-bx2dc Жыл бұрын
I bet not :(
@protox4
@protox4 Жыл бұрын
Yes, this is actually required to get some C# 9 features working. (And even some C# 7 features before they updated the CLR)
@soverain
@soverain Жыл бұрын
Did the trick for init only setters. Don’t know if it would work for other things. Damn I want those file namespaces so bad.
@Rovsau
@Rovsau Жыл бұрын
@@protox4 Are there any drawbacks to doing this? How far have you tested it?
@protox4
@protox4 Жыл бұрын
@@Rovsau The only drawbacks are if you import multiple libraries that also hacked in the language support and you get type clashes. This doesn't work with language features that require actual runtime support and not just metadata (like default interface methods).
@DmitriiP
@DmitriiP Жыл бұрын
.Net Framework v3.5 compact? :)
@adrgri
@adrgri Жыл бұрын
I'd give you a 100 likes for this instead of just one if I could. Thanks so much for sharing this because I am actually stuck on C# 6 right now This almost sounds too good to be true, are there any drawbacks of doing this other than the compilation errors "target runtime doesn't support [...]"?
@asedtf
@asedtf Жыл бұрын
Microsoft: Please stop using .NET Framework! Us: lol no
@egor5622
@egor5622 Жыл бұрын
What's about $"{name}" ?
@nickchapsas
@nickchapsas Жыл бұрын
What about it?
@FernandoTakeshiSato
@FernandoTakeshiSato Жыл бұрын
Super interesting...but I'm kind of afraid of pushing stuff like this to prod :/
@focl2003
@focl2003 Жыл бұрын
Hi bro, are you using it?, any problems?
@FernandoTakeshiSato
@FernandoTakeshiSato Жыл бұрын
@@focl2003 not using it, sorry
@sumittandon6246
@sumittandon6246 Жыл бұрын
Can anyone point me to the package src ?
@nickchapsas
@nickchapsas Жыл бұрын
It’s in the video description
@sumittandon6246
@sumittandon6246 Жыл бұрын
@@nickchapsas Thank you buddy 😉
@hichaeretaqua
@hichaeretaqua Жыл бұрын
In the end, all code is just text that the compiler makes meaning of.
@focl2003
@focl2003 Жыл бұрын
Most of it.
@TechySpeaking
@TechySpeaking Жыл бұрын
First
@BeanCoffeeBean
@BeanCoffeeBean Жыл бұрын
Even though this gives us the advantage to use new C# features, it will sadly not give us the performance boost of newer versions of .NET :(
@ibrahimhussain3248
@ibrahimhussain3248 Жыл бұрын
I think I used somrthing like LangVer=preview on a project before for this
@FabioMaulo
@FabioMaulo Жыл бұрын
Ok but... if you have a 4.7.2 prj the best thing to do is convert it to NET6. We did it in 2022 moving fast converting all libraries to netstandard2.0, then surface prj (web a no web) from 4.7.2 to netcore3.5, and then to NET6. If you have an old prj you have to convert it to the new NET as soon as possible.
@crifox16
@crifox16 Жыл бұрын
how painful was it really? i'm stuck with net framework 4.8 at work so i'd be curious to hear some experiences
@FabioMaulo
@FabioMaulo Жыл бұрын
@@crifox16 it was a process. You have to start moving all base DLL (no EXE/WEB) to netstandard2.0
@oddikaro8236
@oddikaro8236 Жыл бұрын
Why would you want to convert it to .NET 6? I mean... .NET Framework has infinite support while .NET 6 only 1-year more...I don't see any reason if you have a big project in .NET Framework already working. Can you provide your opinion?
@crifox16
@crifox16 Жыл бұрын
@@oddikaro8236 multi platform support is the first thing that comes to mind
@oddikaro8236
@oddikaro8236 Жыл бұрын
@@crifox16 ya..but...why would you need that? Your 10-years app is already working somewhere...
@XxJackDjTomxX
@XxJackDjTomxX Жыл бұрын
Hi Nick, I've seen your amzaing video about foreach loops with Ranges (Adding a BETTER way to loop in C# -> kzbin.info/www/bejne/oJ7Qq2SZftOBpdk) and I tried to loop like this: foreach (var i in -10..-5) but that didn't work since Range doesn't support negative numbers. So I was wondering if you know any way to make this foreach loop work with negative ranges?
@klocugh12
@klocugh12 Жыл бұрын
> Sharpen your skills I see what you did there.
@nickchapsas
@nickchapsas Жыл бұрын
AYE
@brianm1864
@brianm1864 Жыл бұрын
I had to watch this on 1.5 speed because I died a little bit inside thinking about still having to work in .NET Framework
@realharami
@realharami Жыл бұрын
Nick, you know black magic is forbidden, do not you?
@sacharkin
@sacharkin Жыл бұрын
Why you should do this? Get rid of legacy projects and upgrade to the latest .NET version ;)
@nickchapsas
@nickchapsas Жыл бұрын
Sometimes it is impossible to move. No every company can afford the change and it doesn't make sense for every company either
@adrgri
@adrgri Жыл бұрын
@@argamon2025 Why wouldn't it work with third party libraries? Those dependencies are typically separate assemblies, compiled elsewhere 🤔
@jarnohonkanen4321
@jarnohonkanen4321 Жыл бұрын
Get rid of .net framework is the goal but we are stuck because our hosting doesn't support .net core and probably never will... I have been working hard get rid all old legacy to be ready to switch more modern hosting environment but that is not easy job... Too much technical debt and a lot of assumptions about hosting environment details. I'm lucky that I have able to to use lang version 10. I'm haven't heard PollySharp before but I have used IsExternalInit and BackwardsCompatibleFeatures nugets to achieve similar results. I definitely give a try for this.
@OleksiiKorniienko
@OleksiiKorniienko Жыл бұрын
The only drawback of doing this is that I have to install the package in EVERY project in the solution.
@PetrVejchoda
@PetrVejchoda Жыл бұрын
WOW
The fastest way to iterate a List in C# is NOT what you think
13:42
Nick Chapsas
Рет қаралды 154 М.
The New Extensions EVERYTHING Feature of C# 13!
10:32
Nick Chapsas
Рет қаралды 68 М.
Can you beat this impossible game?
00:13
LOL
Рет қаралды 71 МЛН
ТАМАЕВ vs ВЕНГАЛБИ. Самая Быстрая BMW M5 vs CLS 63
1:15:39
Асхаб Тамаев
Рет қаралды 3,3 МЛН
The BEST way to reset your database for testing in .NET
13:14
Nick Chapsas
Рет қаралды 59 М.
How to Deploy Lambda Layers Using the AWS CDK
5:01
Cloudmancer
Рет қаралды 37
Don't throw exceptions in C#. Do this instead
18:13
Nick Chapsas
Рет қаралды 249 М.
Stop using the Process class for CLI interactions in .NET
15:04
Nick Chapsas
Рет қаралды 50 М.
Why I don't use the "else" keyword in my code anymore
20:40
Nick Chapsas
Рет қаралды 334 М.
The New .NET 9 HybridCache That You Must Upgrade To!
14:34
Nick Chapsas
Рет қаралды 43 М.
What is the difference between out and ref parameters ?
0:55
Interview Happy
Рет қаралды 9 М.
Can you beat this impossible game?
00:13
LOL
Рет қаралды 71 МЛН