Intro to Yield in C# - What it is, how to use it, and when it is useful

Поділитися
Вставка
  • Опубліковано 28 вер 2024

КОМЕНТАРІ • 258

  • @RalfsBalodis
    @RalfsBalodis 3 роки тому +58

    0:00 - Intro
    1:10 - Creating console app
    1:58 - What is Yield in C#
    2:34 - Writing demo code
    8:03 - Demo code result analysis
    9:56 - How to use Yield
    13:07 - Debugging Yield
    16:55 - Benefit of using Yield
    20:01 - Limiting Yield returns
    31:57 - Iterating collection of IEnumerable
    36:41 - Why not to Yield results to List
    41:21 - Summary and concluding remarks
    The number 4 in the list of primes is criminal.

    • @tomthelestaff-iamtimcorey7597
      @tomthelestaff-iamtimcorey7597 3 роки тому +3

      Thanks for the work. Folks love this type breakdown.

    • @kiwiproductions4510
      @kiwiproductions4510 2 роки тому

      lol. Yeah the 4 is because he did 'less than' instead of 'less or equal to'. Edit: Also, isn't 1 not a prime number either?

  • @juanminglao3699
    @juanminglao3699 2 роки тому +2

    2 years ago I watched my first programming videos here, it has been my hobby since, everyday. Thank you sir for changing my life.

  • @sergueikhous5873
    @sergueikhous5873 3 роки тому +1

    Also exclude 1,
    if (IsPrimeNumber(counter))
    {
    if (counter != 1)
    {
    yield return counter;
    }

    }
    Thanks Tim for another great video!

    • @ibimslawa
      @ibimslawa 11 місяців тому

      Yeah the IsPrimeNumber function is flawed, cause it spits out 4 but 4 is no prime number (can be divided by 1, 2, 4)

  • @johnaldrin3799
    @johnaldrin3799 3 роки тому +44

    I noticed in your demo that the number "4" was being returned in the answer set. I found that t.he problem was in the FOR loop in IsPrimeNumber. It should read:
    for (int i = 2; i

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому +15

      Yep, that one bugs me. Oh well.

    • @DasTagwerk
      @DasTagwerk 3 роки тому +11

      And you have to exclude 1 - it’s not a primenumber

    • @aljazprosenak4194
      @aljazprosenak4194 3 роки тому +4

      @@DasTagwerk
      var primeNumbers = Generators.GetPrimeNumbers().Skip(1).Take(10000);
      Fixed like a true programmer ...
      edit: Do not run tests with number 1 in mind though ... lul

    • @lukasztomaszek
      @lukasztomaszek 2 роки тому

      Better is:
      int maxDivider = Math.Floor(Math.Sqrt(value)) + 1;
      for (int i=0; i < maxDivider; i++) {...}
      I don't know what better to name a variable in this case.

    • @drstrangelove9851
      @drstrangelove9851 5 днів тому

      That 4 was bothering me. Ha ha.

  • @KanokpitsunLertprommakul
    @KanokpitsunLertprommakul 3 роки тому +2

    I love how to you explained it's very clear for me thank you

    • @Fran4Klin
      @Fran4Klin 3 роки тому +3

      @Onedev Actually in video you have like three examples :)
      And if you actually watched this then you know that it can be pretty usefull when dealing with larger collections while you need only some part of it.
      Like, you create stuff you want from this list on the fly - you don't need to generate whole collection before you get to the item you want

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      Glad it was helpful!

  • @jatingandhi1988
    @jatingandhi1988 2 роки тому

    Too good explanation Tim. Keep it up! Thanks!

  • @CD10ANIL
    @CD10ANIL 2 роки тому

    mostly it is good to know what keys are pressed ,, sometime you confirm the same like cw tab tab, this can be improved in teaching with software like carnac, that removes the explicit telling what keys are pressed, as the same is shown in side of screen and user can pause and see the keys when in doubt what keys are pressed to achieve certain action.

  • @marvinjno-baptiste726
    @marvinjno-baptiste726 3 роки тому

    I should probably move on, but if I have a Linq2SQL DataAccess, is the linq-based data querycouple with a .ToList not so much of an issue as described towards the end of the video?
    I know there's an amount of WHERE setting in the SQL, based on the LINQ but I'm just wondering if there would be any benefit to yielding linq2sql?

    • @AuiAuee
      @AuiAuee 3 роки тому

      Since no one of us designed linq2sql that's quite hard to answer but since you're pulling Data from a Database yielding should benefit in most cases.
      When you're using a function that is leading to results like ToList() or Single() linq2sql trys to execute most of your linq expression or linq function-chain as sql. A result set is created in memory and non sql translatable functions are executed on that memory copy. After that you get the result subset that is the result you're working with in code. using yield form here on has no benefit at all since everything is already in memory.
      BUT! using yield in the linq expression or the function-chain will benefit as long as it's fitting the linq2sql concept.
      So if you're using extension methods heck yes use yield.
      If you're working with an IQueryable or equivalents probably use yield. unless it's obviously cached and there is no existing linq function that helps you around.
      if it's all about performance you should probably work with optimized self-written sql, stored procedures and decisions on server/client workload but that's time expensive and kind of nerdy.

  • @marceloleoncaceres6826
    @marceloleoncaceres6826 11 місяців тому

    Thank you Mr. Corey, I was wondering if a case use of yield could be an application that reports the percentage of advance. For example, if the app has 20 steps and each of these steps is time-consuming, yield could send a notice of the advance, so the user knows that the app is still working.

    • @IAmTimCorey
      @IAmTimCorey  11 місяців тому +1

      That sounds more like a gRPC connection. Yield can do it, but you need to keep asking it for more information. A gRPC connection can send data when it gets it.

    • @marceloleoncaceres6826
      @marceloleoncaceres6826 2 місяці тому

      Thanks a lot for your answer, it means a lot to me.

  • @Kaalund1989
    @Kaalund1989 6 місяців тому +1

    can this be used from a API to a C# WASM so it can render while downloading and dont block the UI?

    • @IAmTimCorey
      @IAmTimCorey  6 місяців тому +1

      Calling an API should always be asynchronous, which means it should not block your UI. Using yield wouldn't change that. I would recommend that you look at your underlying UI to figure out why you are being blocked. Maybe you forgot to call an API method using async.

  • @Aeroxima
    @Aeroxima Рік тому

    Sounds like one of those somewhat more "out there" things that doesn't come up a ton, but does for certain things. Comes up a lot working with games (like in Unity), just like quaternions, which... I can't imagine I'd hear of them anywhere else but maybe something related to gimbal lock, which I also would never hear about if I didn't have to look up quaternions. lol.

    • @IAmTimCorey
      @IAmTimCorey  Рік тому

      It isn't something you will use often, but when you need to use it, it will be really helpful.

    • @changemaker9751
      @changemaker9751 Рік тому

      I am here because i am trying to understand what IEnumerable , IEnumerator and yield keywords does.I am using Unity too, I use coroutines a lot and I came to a conclusion that Coroutine method does what Tim did here(Correct me if i am wrong) .You can select object one by one and do whatever you want with them. You can delay things in seconds via new WaitForSeconds or call after some actions to be done via WaitWhile and WaitUntil(they should be func delegate).I realised that it would be ok if i didnt struggle a lot on IEnumerator but it is always fun to look under the hood.
      Quaternions are always used for rotations .They are quite useful when you should rotate or orient an object and prevent your object from having gimbal lock.

  • @arjundevrana3376
    @arjundevrana3376 3 роки тому

    Hi tim can you make sperate play list on multi threading in c# I have more confusion🤔🤔🤔

  • @Coeur85
    @Coeur85 3 роки тому +2

    Tim I’m confused on how to use yield with dapper when using query async , or over api end points that returns json data !

  • @hosamhemaily4817
    @hosamhemaily4817 Рік тому

    minute 19 is the most important minute

  • @Lior_Banai
    @Lior_Banai 3 роки тому

    how do you handle Resharper warning of multiple enumeration (when using IEnumerable)?

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому +1

      I don't use Resharper. It sounds like a setting that needs to be tweaked, though.

  • @chezchezchezchez
    @chezchezchezchez Рік тому

    So I’ve read all these comments. So far everybody looks at yield as more of an experiment. And nobody’s really using it in code in a useful way.
    I’m waiting for the comment where someone says “you’d be crazy not to use yield in this instance”

    • @IAmTimCorey
      @IAmTimCorey  Рік тому

      It is a limited-use tool. A specialty item. I covered some practical uses in this video, and there are more, but there aren't hard and fast use cases that you will always need yield for, though.

    • @changemaker9751
      @changemaker9751 Рік тому

      In Unity , Game Developers use yield return new waitForSeconds a lot.Thats why i am here to learn where it comes from and i realized that i dont have to do anything extra.Unity takes care of it for me.

  • @asdasddas100
    @asdasddas100 3 роки тому

    Is it okay if I call ToList after I do my computational work in entity framework or should I exclusively work with IEnumerable all the way through my app?

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      It depends on the situation but usually yes.

  • @senatorpoopypants7182
    @senatorpoopypants7182 Рік тому

    Anyone noticed 4 was printed while it was not a prime number, that happened because the loop started at 2 which was equal to 4/2. So the condition wasn't met and it returned true. Which is why I have horrible anxiety that I might have screwed up my code even after thorough testing.

  • @fieryscorpion
    @fieryscorpion 3 роки тому +1

    Have you thought of creating short videos instead of these massively long videos? Not everyone has time and would be great if you could pack the concept in under 10 minute videos.
    Thanks!

    • @tomthelestaff-iamtimcorey7597
      @tomthelestaff-iamtimcorey7597 3 роки тому

      Others offer those type of videos. If you know exactly want you want, those are fine. Tim is targeting his videos for those looking to learn the topic in more dept. He also tries to orient the topic in real world situations.

    • @chezchezchezchez
      @chezchezchezchez 3 роки тому

      I kind of know what you mean though even though we love Tim!

  • @captkalik
    @captkalik 3 роки тому

    Do you have any videos on run time and design time templates aka those tt files

    • @tomthelestaff-iamtimcorey7597
      @tomthelestaff-iamtimcorey7597 3 роки тому

      Not sure if they are covered in this or not. - ua-cam.com/video/cST5TT3OFyg/v-deo.html

    • @captkalik
      @captkalik 3 роки тому

      @@tomthelestaff-iamtimcorey7597 I don’t think so, that video is about working with text files , tt files or t4 templates are usually temple files that can generate c# code at run time or design time

    • @tomthelestaff-iamtimcorey7597
      @tomthelestaff-iamtimcorey7597 3 роки тому

      @@captkalik OK, I have added it to Tim's list of viewer suggestions for his consideration. Thanks for sharing and clarifying.

    • @captkalik
      @captkalik 3 роки тому

      @@tomthelestaff-iamtimcorey7597 awesome thanks

  • @maestrowilliam
    @maestrowilliam 3 роки тому

    what i need to config in order my VS2019 add the paremeter "value" by it self?

    • @tomthelestaff-iamtimcorey7597
      @tomthelestaff-iamtimcorey7597 3 роки тому

      Tim has several videos on Visual Studio - ua-cam.com/users/IAmTimCoreysearch?query=visual

    • @maestrowilliam
      @maestrowilliam 3 роки тому

      @@tomthelestaff-iamtimcorey7597 that doesn't help. I you know the answer or the video just tell me.

    • @tomthelestaff-iamtimcorey7597
      @tomthelestaff-iamtimcorey7597 3 роки тому

      @@maestrowilliam I do not know which video nor do I know which setting. I wish you luck.

  • @aresagathos2075
    @aresagathos2075 Рік тому

    4 is not a prime number....

    • @IAmTimCorey
      @IAmTimCorey  Рік тому

      I forgot to turn on the bug zapper and one got through.

  • @fitstand8115
    @fitstand8115 3 роки тому +42

    Let me say, you DO make learning C# easier sir, thank you so much for making these videos, you can't imagine how much your videos help.

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому +11

      I appreciate the kind words. I am glad my content has been helpful.

  • @changemaker9751
    @changemaker9751 Рік тому +7

    I have watched 5-6 videos about IEnumerable, IEnumerator, yield , ICollection , IList etc. before i saw your video. None of them were crystal clear to me. After i watched this, I stood up and clapped you cause of what you did here. You are straight to the topic and great teacher. THANK YOU

  • @memsom
    @memsom 3 роки тому +12

    IAsyncEnumerable and yield... that is where it is at.

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому +2

      That is a powerful combo.

    • @memsom
      @memsom 3 роки тому

      @@IAmTimCorey it sure is. I turned some code that was a big list of async operations that issued multiple events as they executed, in to a set of generic handlers that were created from a factory and made the execute method return a IAsyncEnumerable so that the events raised can be generated as a sequence and despatched. It is a thing of beauty.

  • @belmiris1371
    @belmiris1371 3 роки тому +10

    I kind of think the yield functionality was added just so employers could use it as a trick question for interviews =)

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      lol there are reasons for it. It just isn’t an everyday thing.

    • @Hantick
      @Hantick 3 роки тому +1

      Haha exactly, I had one

  • @AhmadAlMutawa_abunoor
    @AhmadAlMutawa_abunoor 3 роки тому +7

    I wish you covered .Skip() since it has some relevance with .Take() . Very informative and nice work

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      Thanks!

    • @streammxc
      @streammxc 3 роки тому +1

      If I do .Skip(10).Take(15) will that be as efficient as the iterator version?

    • @giacmoit
      @giacmoit 3 роки тому +1

      @@streammxc It will work the same as iterator.

  • @andrewkeen2456
    @andrewkeen2456 3 роки тому +7

    For those running low power computers: you can make the for loop signature for(int i = 2; i

    • @tomthelestaff-iamtimcorey7597
      @tomthelestaff-iamtimcorey7597 3 роки тому

      Thanks for the tip

    • @gibbytravis
      @gibbytravis 3 роки тому +3

      Could also skip all the even numbers in the GetPrimeNumbers() method, except for 2. The first line can be "yield return 2;". The counter can then be started from 3.

    • @gibbytravis
      @gibbytravis 3 роки тому +5

      public static IEnumerable GetPrimeNumbers()
      {
      yield return 2;
      int counter = 3;
      while(true)
      {
      if (IsPrimeNumber(counter))
      yield return counter;
      counter+=2;
      }
      }

  • @DoctorKrolic
    @DoctorKrolic 3 роки тому +6

    27:30 - are you shure, that 4 is a prime number?)

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому +3

      lol, I missed an equals in the method. Hate it when that happens.

    • @DJDoena
      @DJDoena 3 роки тому

      @@IAmTimCorey Also, technically, 1 is not considered a prime number. 2 is the first (and only even) prime.

    • @chrisauret3785
      @chrisauret3785 3 роки тому

      @@DJDoena wow, so smart

  • @LordErnie
    @LordErnie Рік тому +1

    Wouldn't this also be an example of a decorator? You wrap something in something else, just to extend it's functionality. Something that gives back infinite numbers gets wrapped in an enumerator which will count to 10 and then discard correct? Or does LINQ use some other magic under the hood?

    • @IAmTimCorey
      @IAmTimCorey  Рік тому +1

      Not really. As for what LINQ is doing, it is basically setting up a state machine.

    • @LordErnie
      @LordErnie Рік тому

      @IAmTimCorey Do you have any articles or reads on that? I've been looking for a clear example of what linq does for a while now, but I can't for the life of me get a clear example

  • @Heidtkeja04
    @Heidtkeja04 3 роки тому +2

    Thanks for the video Tim. Don't you have a bug in your function? It returns number 4 within your prime numbers.

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому +2

      Yep, the method is missing an equals sign.

  • @jawadurrehman3487
    @jawadurrehman3487 3 роки тому

    in my visual studio this console.writeline(value:"string"); the word "value" is not showing, how to enable it.

  • @RiversJ
    @RiversJ Рік тому

    One should really clamp numeric iterators with the types MaxValue even for demo code or especially so perhaps?

  • @kokokupelian7926
    @kokokupelian7926 3 роки тому +5

    Youre the best tutor ever☺️

  • @RalfsBalodis
    @RalfsBalodis 3 роки тому +3

    Thanks. Learned about something new today! :)

  • @CSharpAntero
    @CSharpAntero 2 роки тому +1

    Hi Tim, thanks for all the great videos, it is easy to understand and I learned very easy and fast the single topics I'm looking for. I use yield in the game development, using unity engine (c# scripting), when you great game objects, over and over again, thru the game time, you can create new objects and destroy the once you don't need any more, this helps to keep the memory only with game objects you really need and of course with creating them on run time, it speeds things up

  • @markharwood6794
    @markharwood6794 2 роки тому

    Great video, as ever... But 1 isn't a prime number :)

  • @christopherbaldwin1194
    @christopherbaldwin1194 3 роки тому +2

    Hey Tim just wondering how you get the value: and count: hints in the code? I'm not sure I've ever seen that default in my code.

  • @samshtiff5585
    @samshtiff5585 Рік тому

    Thanks, useful video, for secound 15 prime number you could do something like this take(15).skip(10) and 10 is the count of the list so far

  • @GreekkAlex
    @GreekkAlex 2 роки тому

    greetings and appreciation from Kazakhstan!!!(i d'know, you've heard about Kazakhstan at all))) )

  • @neroleso
    @neroleso 3 роки тому +1

    You're my guide in C-Sharp dr.Tim .. No word could express our gratitude to you >> THANK YOU

  • @casinodeathfish8703
    @casinodeathfish8703 3 роки тому +10

    Since when is 4 a Prime?

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому +9

      Since I forgot the equals in the check.

    • @DrorF
      @DrorF Рік тому

      Since we're on the subject, 1 is not prime either.

  • @mileswilliams527
    @mileswilliams527 3 роки тому

    Using Yield is just lazy.
    Sorry, I couldn't help myself.

  • @AlThePal78
    @AlThePal78 3 роки тому

    You sound different from your tutoriala and when you talking and showing your face on camera

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому +1

      It is the same mic for both in the videos from this year. Interesting.

  • @Argidiel
    @Argidiel 2 роки тому +1

    Thanks

  • @ahmed_raaphat
    @ahmed_raaphat 2 місяці тому

    Everything is clear now. Thanks a million!

  • @mikoaj1954
    @mikoaj1954 3 роки тому

    4 is not prime, I was wondering where I went wrong then noticed you made a mistake in a for loop.

  • @Jabroni24601
    @Jabroni24601 3 роки тому +1

    What option flag do you have set to show the argument names for methods being called (e.g. value for Console.WriteLine)?

    • @transtechmuthu
      @transtechmuthu 3 роки тому

      You must have VS2019 - v16.8 and above .. Tools > Options > Text Editor > C# or Basic > Advanced and select Display inline parameter name hints

    • @garethbraid937
      @garethbraid937 3 роки тому

      In VS settings:
      Text Editor > C# > Advanced > Display inline parameter name hints

  • @WayneGreen-g8l
    @WayneGreen-g8l Рік тому

    I wonder what would happen if yield were used inside an async/await operation. I also wonder if yield would/could ever be used with Entity Framework and IQueryable. I'm having a difficult time visualizing yield being used in CRUD operations, even in reads where you just get something and display it. It doesn't sound useful in those situations. Am I missing some things?

  • @nuniezjorge
    @nuniezjorge 3 роки тому

    pure programming goodness without all that hipster bullshit of other channels

  • @anugerahmodif9894
    @anugerahmodif9894 3 роки тому

    great sir,
    i've a question, why.. if i put counter++ inside the if statement with yield return init, the console crash suddenly

  • @brianviktor8212
    @brianviktor8212 3 роки тому +1

    Excellent video. Yield was one of those gaps of knowledge I had for a long time. Can't think of a scenario where I could use it yet, but I am sure I will consider it in the future.

  • @_incarnate
    @_incarnate 2 роки тому

    How can we use this in the case of pagination? Say when I click on next it should fetch 20 records per page, dynamically without duplicating the code. Thanks

  • @hannukoistinen5329
    @hannukoistinen5329 3 роки тому

    Could you also show, how to make a real application? Why use just var and not int, double etc.?

  • @Voodookillers
    @Voodookillers 2 роки тому

    When I press Result View, I do not get the error message, instead it suddenly instantiates all the people, regardless of debugging. I THINK this might be because of the newer C# version or Visual Studio 2022. So it seems this has changed. It confused me for a bit.

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому

      Are you using my code that you downloaded? It may be a slight difference compared to my code if you typed it yourself.

    • @Voodookillers
      @Voodookillers 2 роки тому +1

      @@IAmTimCorey Hi! Thanks for replying. I just downloaded your source code and on line 13 removed to Take(2), so just: var people = DataAccess.GetPeople(); I started debugging and when I checked the Result View of people in debug mode it once again instantiated all 3 people, and logging them to the console. If I don't check the result view, it will have the wanted result, so only instantiating them one at a time in the foreach section. I am using Visual Studio Community 2022 V 17.2.0

    • @TheInkSpoots
      @TheInkSpoots Рік тому

      @@Voodookillers This is really strange, i get the same "Issue" cool that you also pointed it out.

  • @mehrtashsouri6862
    @mehrtashsouri6862 2 роки тому

    another GEM from dear uncle Tim.

  • @gibbytravis
    @gibbytravis 3 роки тому

    1 is not prime. Dislike, unsub. JK

  • @Xershade
    @Xershade 2 роки тому

    Instead of doing while(true) you should use while (counter < int.max) simply because even with the yield, eventually you WILL overflow and throw an exception if the yield is called enough times. Also once the loop breaks the next count will start over at the first number if it does overflow instead of just burning the application in overflow exception fire.
    Though still do NOT use that without the yield people, it will basically lock up and run forever essentially like while(true) would it just won't overflow and kill the app.

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому

      In this case, we will never reach an overflow state. Instead, we will reach a computational limit trying to compute that large of a prime number. Changing to the evaluation won't change that so the comparison is not necessary, and it adds overhead.

  • @heuristix77
    @heuristix77 9 місяців тому

    1:16 Blazor Server supremacy 🙌

  • @JasonWynn
    @JasonWynn 3 роки тому

    I've never wrote a program using yield but I had to work on one written by someone else. This program compared files in two folder structures and copied the files that were different from the source folder to the destination folder. Having never seen this before, I stepped through the code with breakpoints. The order of the calls was really weird to me at first (still is I guess, lol) but then I saw that this way was a lot more efficient than reading everything to to a list and then operating on the list.
    Great explanation Tim!

  • @marcusreinicke
    @marcusreinicke 3 роки тому

    Hi Tim, THX for your explain.
    Its how ever great!!
    But i have a Question.
    How can I use the yield Iterator over the Network. With REST or everything else.
    Can you explain more of this topic?
    THX
    Marcus

  • @rusektor
    @rusektor 3 роки тому

    I wonder why on earth you would need "else { break; }" at 35:00?

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      This is a safety check to be sure we can move next. The "infinite" prime number method should never return that if it is being called without modifiers but if we limit it to be called only a certain number of times, it would run out of times at that point and not be able to move next. For instance, if in this case we had limited the call to 5 times, it would hit the break before the end of the loop.

    • @rusektor
      @rusektor 3 роки тому

      @@IAmTimCorey Beg your pardon, but I didn't understand you. The decision whether to break or not is controlled solely by "MoveNext()" method. Where were you about to put the limit (as you said - 5 times)?

  • @ManderO9
    @ManderO9 3 роки тому

    27:46 yo who else noticed the four lol

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      Yep. I forgot an equals in the evaluation.

  • @phanuwatwattanapraya7998
    @phanuwatwattanapraya7998 2 роки тому

    So useful, thank you so much

  • @mohammedelsuissey1745
    @mohammedelsuissey1745 3 роки тому

    There is a mistake here, I'm no mathematician but (4) is not prime number :)
    Good session though your method of explaining and teaching is amazing
    Thanks sir...

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      lol yeah, kicking myself for missing that one. You need to add an equals in the for loop to fix this.

  • @ahmedalshmary9719
    @ahmedalshmary9719 3 роки тому

    Thank you for the information

  • @vencislavvidov
    @vencislavvidov 6 місяців тому

    is 4 prime number? time 27:43

    • @IAmTimCorey
      @IAmTimCorey  6 місяців тому

      Nope, that's a bug in the code. We should have either checked for that specific case or done a "i

  • @Anton-Os
    @Anton-Os Рік тому

    Thank you very much!!!

  • @chrisroberts1773
    @chrisroberts1773 3 роки тому

    I'm confused about how that isn't a terrible prime number checker. Skip even numbers and memo-ize that and it might be "not terrible".

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      It isn't terrible, but as I stated in the video, it isn't the best. That wasn't the goal. The goal was to have a bit of code where it made sense to call it repeatedly but where we could not efficiently anticipate how many times it needed to be called.

  • @LarryPeteet
    @LarryPeteet 3 роки тому

    Regarding running out of memory in the GetPrime without a Yield:
    Wouldn't it just return 3 each time it is called?

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      No, it will return a list of prime numbers but it does not know when to stop finding them. Give it a try.

  • @jasonhoi85
    @jasonhoi85 3 роки тому

    why 4 is a prime number?

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      Because I forgot an equals in the evaluation.

  • @kuldeepjain7976
    @kuldeepjain7976 3 роки тому

    Very powerful Yield

  • @talon4107
    @talon4107 3 роки тому +5

    Would you do video about REGEX/REGEXP in C#?

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому +3

      I will add it to the list. Thanks for the suggestion.

  • @detk1874
    @detk1874 2 роки тому

    very nice yield demo

  • @jasonfigueroa9867
    @jasonfigueroa9867 Рік тому

    This finally clicked for me. Thank you for an awesome explanation!

  • @hadialikhani-s3q
    @hadialikhani-s3q 7 місяців тому

    Amazing 🤩

  • @henry-js
    @henry-js 3 роки тому

    Hi Tim, should dependency injection be used for everything now?

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      Most things, yes. It is a very useful pattern.

  • @canny255
    @canny255 3 роки тому

    Feature 'target-typed object creation' is not available in C# 7.3. Please use language version 9.0 or greater!
    Got this error from the line
    List output = new();
    But showed the output!! 🤔

    • @AuiAuee
      @AuiAuee 3 роки тому

      = new() is a rather new language feature. In old c# you would always write = new List()

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      Interesting.

  • @TheAngelOfDeath01
    @TheAngelOfDeath01 2 роки тому

    yield return is a very powerful piece of functionality. Do upon request; not do upon load which in some instances can be great. But this is definitely also something that needs to be used with care and consideration, because it does a lot of things while looping through that foreach()-loop, and if you do multiple nested loops this could potentially slow down things than one might expect, particularly if you're working with recordsets from a database, etc.
    And Corey, you've gone done the perfect Terminator imitation! It will not stop. EVER -- until your PC is idea!
    Do you want to kill somebody's computer with that while()-statement? ;-) It's not gonna kill the PC, I know, BUT it's gonna be a busy bunny for a bit -- without the yield return. ;-)

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому +1

      The code that the yield executes is no different than "regular" code in how you work with it. If you do lots of nested loops, that's going to be an issue whether you are yielding the results of the top loop or not.

    • @TheAngelOfDeath01
      @TheAngelOfDeath01 2 роки тому

      @@IAmTimCorey Oh, I know. I was referering to doing what you were coding inside nested loops. Nested loops is ALWAYS a performance issue, especially when they are forever enduring.

  • @not2envy
    @not2envy 3 роки тому

    @IAMTimCorey how did you enable the class/type in visual studios? at Console.WriteLine(value: "Start of the App"); @timestamp: 7:52 ?

    • @not2envy
      @not2envy 3 роки тому

      im talking about the "value:" part.

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      Here you go: ua-cam.com/video/f0YeVirKPfw/v-deo.html

  • @DuncanSungWKim
    @DuncanSungWKim 2 роки тому

    What about "yield break"?

    • @IAmTimCorey
      @IAmTimCorey  2 роки тому

      What about it?

    • @DuncanSungWKim
      @DuncanSungWKim 2 роки тому

      @@IAmTimCorey I wish you explained "yield break" too, which I believe is as important as "yield return"

  • @crazyfox55
    @crazyfox55 3 роки тому

    You should have went into more about how paging works. How does the server remember where to resume if the second page is loaded with a REST API. Also going over the Skip Linq command would have been helpful to show new users.

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      This video was about Yield, not APIs or LINQ which is why I didn’t cover more about those.

    • @crazyfox55
      @crazyfox55 3 роки тому +1

      @@IAmTimCorey I think Skip is an important distinction here because it still creates/calculates the objects even though they are skipped. Also ILspy could help here, to give people a way to debug their code better.

  • @josephang9927
    @josephang9927 3 роки тому

    Y'ALL in C#

  • @BoilermakerRV
    @BoilermakerRV 2 роки тому

    Thanks!

  • @markovovk5391
    @markovovk5391 2 роки тому

    Thanks

  • @UmanPC
    @UmanPC 3 роки тому

    GREAT!

  • @ivansamohvalov440
    @ivansamohvalov440 3 роки тому

    Does anyone know how to switch fast between highlighted variables as Tim did at 22:14?

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому

      When you use a snippet, you can tab between different spots.

    • @ivansamohvalov440
      @ivansamohvalov440 3 роки тому

      @@IAmTimCorey thank you so much!!!

  • @SocialExde
    @SocialExde 3 роки тому

    Nice video. Do you have an example with SqlDatabase and Dapper?

  • @AlexxXRecorD
    @AlexxXRecorD 2 роки тому

    Very very useful lesson. Thanks so much!!!!!!!

  • @AlThePal78
    @AlThePal78 3 роки тому

    Have you ever explained what a dto is and when n how to use it ?

  • @csevaibhavjain
    @csevaibhavjain 3 роки тому

    Can API endpoint return as Yeild to the caller?

    • @IAmTimCorey
      @IAmTimCorey  3 роки тому +1

      Not directly but it can use yield internally so it acts the same way. Just remember that this would be for everyone, not per user.

  • @projectptube
    @projectptube 3 роки тому

    lol @ "interator"

  • @danixadem
    @danixadem 2 роки тому

    absolutely great explanation of the topic !!

  • @michelchaghoury870
    @michelchaghoury870 3 роки тому

    gr8 vid like always, tnx a lottt keep going

  • @gower1973
    @gower1973 3 роки тому

    Is an IEnumerable a dynamic array? Like the vector class in c++?

    • @AuiAuee
      @AuiAuee 3 роки тому

      It may be, but must not. IEnumerable is just an Instance of something you can iterate through like a set, an array or a list. so your Vector class from c++ would probably implement IEnumerable if and only if it was c#

    • @framepointer
      @framepointer 3 роки тому

      It's kinda like implementing std::begin and std::end for use with range based for loops. A std::vector is more similar to a List in C#.

  • @maxron6514
    @maxron6514 3 роки тому

    Tim Sharp and Yield is just what i needed

  • @anastaciu
    @anastaciu 3 роки тому

    6:27 Ctrl+D also does the trick.

  • @istovall2624
    @istovall2624 3 роки тому

    Hah was just reading about this, yay

  • @yanaraldaghestani6305
    @yanaraldaghestani6305 3 роки тому

    amazing into to Yield
    thanks man

  • @dracla26
    @dracla26 3 роки тому

    Tim you DO NOT MISS