New things to .Net4.0 -Lazy<T> - Tuple
1. Tuple
我记得F#中也有类似的数据结构
Tupletuple = new Tuple ("item1", "item2", 20); //Tuple tuple = Tuple.Create ("item1", "item2", 20); var date = Tuple.Create ("item1", "item2", 20); Console.WriteLine(tuple.Item1); Console.WriteLine(tuple.Item2); Console.WriteLine(tuple.Item3);
当然还可以有更多的item,
public static Tuple
2. Lazy<T>
他的出现解决了大对象加载问题,但是是否有实际意义....我见过不少朋友都自己实现过~而且并不是很复杂, 不过既然封装了当然也就有了它的价值
假象大对象:
[Serializable] class INeedLazy { public INeedLazy(string name, string age) { this.Name = name; this.Age = age; } public string Name { get; private set; } public string Age { get; private set; } public void Test() { Console.WriteLine("{0} is {1} years old, he is lazy!", Name, Age); } }
lazy加载此对象
LazylazyObj = new Lazy (() => { return new INeedLazy("Ryan", "2"); }); Console.WriteLine(lazyObj.IsValueCreated); //false lazyObj.Value.Test(); Console.WriteLine(lazyObj.IsValueCreated); //true Console.WriteLine(lazyObj.Value.Name);
或者lazy加载一个string玩玩
var lazyString = new Lazy(() => { // Here you can do some complex processing // and then return a value. Console.Write("Inside lazy loader"); return "Lazy loading!"; }); Console.Write("Is value created: "); Console.WriteLine(lazyString.IsValueCreated); Console.Write("Value: "); Console.WriteLine(lazyString.Value); Console.Write("Value again: "); Console.WriteLine(lazyString.Value); Console.Write("Is value created: "); Console.WriteLine(lazyString.IsValueCreated);