Monday, June 6, 2016

use lazy in C#.Net

//lazy used in C#.net
using System;

class Test
{
    int[] _array;
    public Test()
    {
Console.WriteLine("Test()");
_array = new int[9];
    }
    public int Length
    {
get
{
   return _array.Length;
}
    }
}

class Program
{
    static void Main()
    {

Lazy<Test> lazy = new Lazy<Test>();


Console.WriteLine("IsValueCreated = {0}", lazy.IsValueCreated);


Test test = lazy.Value;


Console.WriteLine("IsValueCreated = {0}", lazy.IsValueCreated);

// The object can be used.
Console.WriteLine("Length = {0}", test.Length);
    }
}

====================================================

Output

IsValueCreated = False
Test()
IsValueCreated = True
Length = 10

No comments: