Search code examples
c#.netlazy-evaluation

Why Lazy<T> forces initialization during serialization?


When I checked the implementation of Lazy<T> class, I saw this block:

[OnSerializing]
private void OnSerializing(StreamingContext context)  
{  
     T obj = this.Value;  
} 

As you can see it's forcing initialization during serialization. Does anyone know why this behavior is preferred as default?


Solution

  • if it doesnt and you provided lambda for initialization, where do you think it retrives the value on deserializing? lambdas are not serializable.