using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lazy
{
class Program
{
static void Main(string[] args)
{
School aa = new School();
List<student> ass=aa.lazy_students.Value;
foreach (var m in ass)
{
Console.WriteLine("姓名:{0} 年龄:{1},时间:{2}",m.name,m.age,m.date);
}
Console.ReadKey();
}
}
public class School
{
public Lazy<List<student>> lazy_students { get; set; }
public School()
{
lazy_students = new Lazy<List<student>>(() =>this.SetLazy());
}
private List<student> SetLazy()
{
List<student> ss = new List<student>{
new student{name="sx",age=18,date=DateTime.Now},
new student{name="sb",age=19,date=DateTime.Now}
};
return ss;
}
}
public class student
{
public string name { get; set; }
public int age { get; set; }
public DateTime date { get; set; }
}
}
C# 延迟处理类 Lazy
最新推荐文章于 2025-08-11 22:40:30 发布
该博客演示了在C#中如何使用Lazy<T>类来延迟初始化一个学生列表,以提高程序性能。在Main方法中,通过School类的lazy_students属性访问并打印出学生信息,显示了姓名、年龄和日期。Lazy加载是一种优化策略,可避免不必要的资源消耗。
1363

被折叠的 条评论
为什么被折叠?



