Lazy Evaluation Feature in C# 4.0(VS2010)
Posted On Sunday, June 7, 2009 at at 2:28 PM by test
Lazy Evaluation Feature in C#
In any programming language , there can be two types of evaluation supported
1: Eager evaluation
2: Lazy evaluation
What is Eager evaluation?
Eager evaluation is the evaluation of the expressions in any programming language without checking its usage in the program whether that expression value is being used in the program or not . For example
Int x = 2 ;
Int y = 3;
Int add( x , x+y ){
Return x }
In above given function , we eagerly evaluated the x+y which is a parameter to the function which is never used in the function body.
What is Lazy evaluation?
Lazy evaluation says that the evaluation of expression should not be performed until it is required in the program
Lets take the same example
Int x = 2 ;
Int y = 3;
Int add(x , x+y){return x;}
In this case, programming language supports lazy evaluation than the argument passed to the ADD function/method will not be evaluated as it is not used in the function.
How Lazy evaluation is supported by programming languages ?
There are two ways to implement the Lazy evaluation in programming languages
1: Language implicitly supports this feature than programmer is free from these concerns
2: Language provides some syntax or library to support the Lazy evaluation .
In C#4.0, second approach is used to support the lazy evaluation
.Net Framework 4.0 provides us with a new class called Lazy. As documentation states then Lazy provides support for several common patterns of lazy initialization, including the ability to initialize value types and to use null values. So it is construct that helps us implement lazy loading.
This is code from MSDN
using System.Linq;
public class Lazy {
private Func func;
private T result;
private bool hasValue;
public Lazy(Func func) {
this.func = func;
this.hasValue = false;
}
public T Value {
get {
if (!this.hasValue) {
this.result = this.func();
this.hasValue = true;
}
return this.result;
}
}
.
The class has three fields:
Func field: is a delegate that will be executed when someone will access the value,
hasValue field: is a logical value saying whether we already called the delegate
result field :stores the result after calling the delegate.
Value property:The logic of the class is in the Value property, which returns a value calculated by the delegate. The getter of the property first tests if we already have the result, and if not, it calls the delegate and stores the result. Otherwise we can just return the result that was calculated earlier(doesn’t it seem like use of Singleton pattern)
Hello World Example
Lazy lazy = new Lazy(() => {
Console.WriteLine("Running for first time only afterwards it caches the result for future invocations...");
Return “Hello World”;
});
Console.WriteLine("Starting...");
Console.WriteLine("output = {0}", lazy.Value);
Console.WriteLine("Output (again) = {0}", lazy.Value);
When we access lazy.value , it executes the func delegate for the first time and caches it result for future calls and in the second call , it returns the same result and hence it also increased performance because of caching . It can be used where we have to a complex calucation and you don’t know 100% that whether you will use it or not .
Happy Coding
In any programming language , there can be two types of evaluation supported
1: Eager evaluation
2: Lazy evaluation
What is Eager evaluation?
Eager evaluation is the evaluation of the expressions in any programming language without checking its usage in the program whether that expression value is being used in the program or not . For example
Int x = 2 ;
Int y = 3;
Int add( x , x+y ){
Return x }
In above given function , we eagerly evaluated the x+y which is a parameter to the function which is never used in the function body.
What is Lazy evaluation?
Lazy evaluation says that the evaluation of expression should not be performed until it is required in the program
Lets take the same example
Int x = 2 ;
Int y = 3;
Int add(x , x+y){return x;}
In this case, programming language supports lazy evaluation than the argument passed to the ADD function/method will not be evaluated as it is not used in the function.
How Lazy evaluation is supported by programming languages ?
There are two ways to implement the Lazy evaluation in programming languages
1: Language implicitly supports this feature than programmer is free from these concerns
2: Language provides some syntax or library to support the Lazy evaluation .
In C#4.0, second approach is used to support the lazy evaluation
.Net Framework 4.0 provides us with a new class called Lazy
This is code from MSDN
using System.Linq;
public class Lazy
private Func
private T result;
private bool hasValue;
public Lazy(Func
this.func = func;
this.hasValue = false;
}
public T Value {
get {
if (!this.hasValue) {
this.result = this.func();
this.hasValue = true;
}
return this.result;
}
}
.
The class has three fields:
Func field: is a delegate that will be executed when someone will access the value,
hasValue field: is a logical value saying whether we already called the delegate
result field :stores the result after calling the delegate.
Value property:The logic of the class is in the Value property, which returns a value calculated by the delegate. The getter of the property first tests if we already have the result, and if not, it calls the delegate and stores the result. Otherwise we can just return the result that was calculated earlier(doesn’t it seem like use of Singleton pattern)
Hello World Example
Lazy
Console.WriteLine("Running for first time only afterwards it caches the result for future invocations...");
Return “Hello World”;
});
Console.WriteLine("Starting...");
Console.WriteLine("output = {0}", lazy.Value);
Console.WriteLine("Output (again) = {0}", lazy.Value);
When we access lazy.value , it executes the func delegate for the first time and caches it result for future calls and in the second call , it returns the same result and hence it also increased performance because of caching . It can be used where we have to a complex calucation and you don’t know 100% that whether you will use it or not .
Happy Coding
I think that lazy evaluation is way cool. It pays to think about it carefully, though. For instance, if you want to see if there are more than three items in an IEnumerable, I think the best way is ien.Skip(3).Any() which should only enumerate 3 or possibly 4 elements. Using the more intuitive ien.Count() > 3 will cause all the elements to be calculated when at most 4 were necessary.