In one website I read:
Lady loading is the concept of delaying the loading of an object until we need this data. In other words, we load the object on demand, rather than unnecessarily loading data earlier.
I want to try using Lazy Loading in my simple console app.
Student Class
public class Students
{
public string Name { get; set; }
public string Surname { get; set; }
public string PESEL { get; set; }
private Lazy<List<Students>> LazyStudents;
public Students()
{
}
public List<Students> StudentsProperty
{
get
{
if (LazyStudents == null)
LazyStudents = new Lazy<List<Students>>(() => LoadStudensList());
return LazyStudents.Value;
}
}
public List<Students> LoadStudensList()
{
List<Students> tempStudentsList = new List<Students>();
tempStudentsList.Add(new Students() { Name = "Adam", Surname = "Wróbel", PESEL = "96120904999" });
tempStudentsList.Add(new Students() { Name = "Edyta", Surname = "Urbańczyk", PESEL = "76354736458" });
tempStudentsList.Add(new Students() { Name = "Gabriela", Surname = "Rydwańska", PESEL = "72637284923" });
tempStudentsList.Add(new Students() { Name = "Dawid", Surname = "Rytel", PESEL = "62736482732" });
return tempStudentsList;
}
}
Program and Main() method:
class Program
{
static void Main(string[] args)
{
Students students = new Students();
Console.WriteLine("Hello World!");
foreach (var items in students.StudentsProperty)
{
Console.WriteLine($"Imię: {items.Name}");
Console.WriteLine($"Nazwisko: {items.Surname}");
Console.WriteLine($"PESEL: {items.PESEL}");
Console.WriteLine("");
}
Console.WriteLine("Hello World!");
}
}
I suppose that i use Lazy Loading, when i create new object of Students class StudentsProperty (return elements from Lazy List) still empty. Elements to LazyList will be added when i use method using StudentsProperty
foreach (var items in students.StudentsProperty)
{
Console.WriteLine($"Imię: {items.Name}");
Console.WriteLine($"Nazwisko: {items.Surname}");
Console.WriteLine($"PESEL: {items.PESEL}");
Console.WriteLine("");
}
I Add breakpoint before foreach loop and i see that StudentsProperty has elements:

Have I implemented lazy loading incorrectly or do I understand something wrong?
Lazy<T>class by deferring instantiation of it. It already handles the lazy aspect, but you are also doing that yourself by checking for null and instantiating it inStudentsProperty. You should instantiate theLazy<T>at the field's point of declaration. As for why you can see theStudentsPropertyproperty - the debugger is accessing it and so of course it gets instantiated, because the debugger is causing that!Lazymeans you no longer need any kind ofnullcheck -- just doreadonly Lazy<..> = new Lazy(...). Creating aLazylazily is superfluous. And second -- at the breakpoint, if you ask the debugger forStudentsProperty-- you're asking for the data, so of course it appears! Set a breakpoint only inLoadStudensListwithout inspecting expressions, step overMain, and see when it's called.Lazy<T>is basically just a shortcut forif (x == null) x = new List<T>(). As others already noted, you can remove your null check.