Thursday, August 06, 2009

Anonymous Example In .Net 3.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Lazy m_lazyInstance2 =
new Lazy(delegate { return new BigInstance(); });

BigInstance sdy = m_lazyInstance2.Value;


Lazy m_lazyInstance3 =
new Lazy(delegate { return new BigInstance2(); });

BigInstance2 sdy2 = m_lazyInstance3.Value;
}

}

public class BigInstance
{

}

public class BigInstance2
{

}

public class Lazy
{
public Lazy(Func func)
{
this.m_initialized = false;
this.m_func = func;
this.m_mutex = new object();
}

private Func m_func;

private bool m_initialized;
private object m_mutex;
private T m_value;

public T Value
{
get
{
if (!this.m_initialized)
{
lock (this.m_mutex)
{
if (!this.m_initialized)
{
this.m_value = this.m_func();
this.m_func = null;
this.m_initialized = true;
}
}
}

return this.m_value;
}
}
}


}

0 Comments:

Post a Comment

<< Home