When to use ValueTask instead of Task and save precious memory in C#

  Рет қаралды 48,703

Nick Chapsas

Nick Chapsas

3 жыл бұрын

Become a Patreon and get source code access: / nickchapsas
Check out my courses: dometrain.com
Hello everybody I'm Nick and in this video I show you how you can use ValueTask instead of Task in your .NET code and save memory. I will talk about one of the most common usecases that pretty much everyone can benefit from and release pressure from the garbage collector and the heap.
Understanding the Whys, Whats, and Whens of ValueTask: devblogs.microsoft.com/dotnet...
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
#dotnet #await #async

Пікірлер: 41
@TedFanat
@TedFanat 3 жыл бұрын
Finally, I found someone who uses a youtube channel to "walk walks and not just talk talks". Really interesting and good explained
@fr3ddyfr3sh
@fr3ddyfr3sh Жыл бұрын
Damn, I’m doing C# for 10 years, and Nick knows his stuff. Short, to the point Videos, perfect for learning or bringing something back you knew once.
@aliascross
@aliascross 3 жыл бұрын
Happy Birthday Nick 😁👍
@bilbobaggins601
@bilbobaggins601 3 жыл бұрын
Another banger of a video! Im still a beginner to C#, but i always watch your videos because they are instructional and interesting. One question, im not sure if you did it already but can you maybe make a video on delegates and events and where to use them. Im pretty sure i understand what they are but i struggle when to use them exactly and correctly. Cheers
@oganovdavid
@oganovdavid 3 жыл бұрын
Happy birthday, Nick! Keep up 💪
@MadMathMike
@MadMathMike 3 жыл бұрын
Excellent demonstration! Thanks for sharing! 😊
@pablocom
@pablocom 2 жыл бұрын
Thanks for the video again NIck! We found several places on our codebase where we can avoid non necessary memory allocations
@HuntsWorkshop
@HuntsWorkshop 3 жыл бұрын
Great video man. Whoever thumbs down your videos just doesn't understand your genius. Their loss.
@InshuMussu
@InshuMussu 5 ай бұрын
ValueTask was always confusing for me but from 8:44 at your video solved my problem. thanks
@bilalmehrban
@bilalmehrban 2 жыл бұрын
Thank you Nick 💕💕 wonderfull explanation.
@eliranbl
@eliranbl 3 жыл бұрын
Thank you for all videos
@adoulou340
@adoulou340 3 жыл бұрын
Great Video ! thanks
@bahtiyarozdere9303
@bahtiyarozdere9303 3 жыл бұрын
Hello Nick. Thanks for another amazing video. I want to ask, do you use Aspect Oriented Programming?
@nickchapsas
@nickchapsas 3 жыл бұрын
I don't mainly because C# doesn't natively support it. I know there are a few ways around that but I think in general there are different ways to tackle what AOP brings to the table, which I find more appropriate in C#.
@Netpercentage
@Netpercentage 2 жыл бұрын
Hey dude... Your videos are the bomb... I have noticed the way you use benchmarks and how your code is refactored, its awosome. Man... I have been trying to get these benchmarks on my business logic layer but no luck... I have the business layer dll inside the console app of the same solution. There are no videos showing real scenario use cases. I want to refactor code in the business logic... Are you able to cook us one of them videos my guy... I believe that would be the first
@granmasterlincoln
@granmasterlincoln 3 жыл бұрын
Great video Nick, as expected. I know that doesn't have any relation to coding, but would be cool a video showing your setup.
@MsbowElite
@MsbowElite 3 жыл бұрын
Yeah, btw we already know that he is using Ryzen 9 3900X, haha.
@nickchapsas
@nickchapsas 3 жыл бұрын
I don't think I will be making a dedicated video on this but I have create an amazon page with all my current gear in case you're interested in that: www.amazon.co.uk/shop/nickchapsas?listId=1E7ERJL49JJW
@davidwilliss5555
@davidwilliss5555 3 жыл бұрын
I have a question: You call the MemoryCache.Set method and pass a TimeSpan. I don't see a Set that takes a TimeSpan. Do you have an extension method that wraps that into a CacheItemPolicy with a sliding expiration or is there some other hidden magic involved?
@kumar315
@kumar315 5 ай бұрын
cool stuff...just wanted to check if using a value task on unit tests would be a good idea?..Since we always mock the external API calls is it a good place to use value tasks everywhere with unit tests ?
@vasugupta1
@vasugupta1 3 жыл бұрын
Great work
@twiksify
@twiksify 2 жыл бұрын
Would you say that using ValueTuple over Tuple has similar memory benefits? Assuming that it's used in a context that allows for stack allocation.
@anishnjain1
@anishnjain1 9 ай бұрын
great job
@SealedKiller
@SealedKiller 3 жыл бұрын
What font do you use in the editor? And if it's JetBrains Mono, how do you get the 0 with the line and not the dot?
@nickchapsas
@nickchapsas 3 жыл бұрын
It’s JetBrains Rider
@shustypl
@shustypl 3 жыл бұрын
I am curious about the result of the same test if you would remove the caching. How much performance affects the use of ValueTask, where Task should be used for I/O operations (calling api or db).
@nickchapsas
@nickchapsas 3 жыл бұрын
ValueTask is slightly slower by nature but always using An actually awaitable task will allocate a bit more if you used ValueTask there.
@ValueLevit
@ValueLevit 3 жыл бұрын
Thanks
@lucaciandrei
@lucaciandrei Жыл бұрын
I would say, that perhaps, if in your class, you have multiple private methods that are returning a Task, you can switch them up to a ValueTask, as the methods themselves will be used inside the object's lifecycle only.
@roflex2
@roflex2 3 жыл бұрын
I know you linked the why and how. But it would be nice to explain why you can't await it twice in the video.
@nickchapsas
@nickchapsas 3 жыл бұрын
Yeah originally I thought the video would be bigger so I didn't explain it because I thought it would inflate it more but you're right, I could give a short explanation. Noted.
@roflex2
@roflex2 3 жыл бұрын
@@nickchapsas love your videos and channel. Keep it up, you are the only person I've seen doing regular c# content
@OlegKosmakov
@OlegKosmakov 3 жыл бұрын
As far as I am aware, since the ValueTask is used in high-performance low-overhead kind of scenarios, it is implemented in a way that it will reuse the ValueTask structure after the result is awaited, and later it might contain results of completely different calculation. That's why awaiting twice is undefined behavior.
@roflex2
@roflex2 3 жыл бұрын
@@nickchapsas I'd love to see a topic on WPF/data binding with uncommonly used things eg BindingOperations.EnableCollectionSynchronization
@protox4
@protox4 2 жыл бұрын
Short answer: because ValueTask can be backed by a custom-implemented IValueTaskSource which may not support multiple awaits.
@ArnabDeveloper
@ArnabDeveloper 2 жыл бұрын
Nice
@kreide847
@kreide847 3 жыл бұрын
Would it be possible for you to open a Discord server for your community?
@nickchapsas
@nickchapsas 3 жыл бұрын
I am currently thinking about how I can set it up without being too much of a hassle. Once I sort that out I will let everyone know
@slowjocrow6451
@slowjocrow6451 8 ай бұрын
I don't understand when you say you can only await it once. If the cache expires you have to await it again... Isn't that "more than once"?
What are record types in C# and how they ACTUALLY work
15:36
Nick Chapsas
Рет қаралды 116 М.
Where are types allocated in .NET and why people get it so wrong
14:35
Эффект Карбонаро и бесконечное пиво
01:00
История одного вокалиста
Рет қаралды 6 МЛН
Black Magic 🪄 by Petkit Pura Max #cat #cats
00:38
Sonyakisa8 TT
Рет қаралды 37 МЛН
1❤️
00:20
すしらーめん《りく》
Рет қаралды 33 МЛН
Understanding how to use Task and ValueTask
26:59
Microsoft Developer
Рет қаралды 26 М.
Stop using LINQ to order your primitive collections in C#
14:57
Nick Chapsas
Рет қаралды 96 М.
C# Async Yield Return: IAsyncEnumerable will change your life!!!
10:13
What are the Frozen Collections coming in .NET?
14:11
Nick Chapsas
Рет қаралды 48 М.
How IDisposable and Using Statements Work Together in C#
10:01
IAmTimCorey
Рет қаралды 28 М.
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 247 М.
microsoft's new AI feature is an absolute dumpster fire
9:34
Low Level Learning
Рет қаралды 26 М.
.NET 9 Fixed Exceptions but Still Don’t Use Them
10:01
Nick Chapsas
Рет қаралды 39 М.
Эффект Карбонаро и бесконечное пиво
01:00
История одного вокалиста
Рет қаралды 6 МЛН