When to lazy load?

后端 未结 8 1832
庸人自扰
庸人自扰 2020-12-31 17:14

I lazy load all my members. I have been doing this for a while and simply taken lazy load to be a good thing at face value.

Let\'s say we have

publi         


        
相关标签:
8条回答
  • 2020-12-31 17:47
    List<int> number = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
            var result = number.Where(x => x % 2 == 0);
            number.Add(20);
            //foreach (int item in result)
            //{
            //    Console.WriteLine("loop1:" + item);
            //}
    
            foreach (int item in result)
            {
                if (item == 4)
                    break;
                Console.WriteLine("loop2:" + item);
            }
            number.Add(40);
            foreach (int item in result)
            {
    
                Console.WriteLine("loop3:"+item);
            }
            Console.ReadLine();
    

    uncomment first loop and see the difference. very using example to under stand deffred execution and lazy loading.

    0 讨论(0)
  • 2020-12-31 17:48

    Lazy loading is a concept where we delay the loading of the object until the point where we need it. Putting in simple words, on demand object loading rather than loading objects unnecessarily.

    For example, consider the below example where we have a simple Customer class and this Customer class has many Order objects inside it. Have a close look at the constructor of the Customer class. When the Customer object is created it also loads the Order object at that moment. So even if we need or do not need the Order object, it’s still loaded.

    Link to Example

    0 讨论(0)
提交回复
热议问题