Can you figure it out? Functional caching

time to read 1 min | 109 words

Okay, here is a challenge, without running the code, what do you think should be the result of this program, and why?

public class Program
{
	private static void Main(string[] args)
	{
		Fetch<int> method = Get;
		method = AddCaching(method);

		method();
		method();
		method();
	}

	public static int Get()
	{
		Console.WriteLine("Called");
		return 1;
	}

	public delegate T Fetch<T>();

	public static Fetch<T> AddCaching<T>(Fetch<T> fetch)
	{
		T cachedObject = default(T);
		bool isCached = false;
		return delegate
		{
			if (isCached == false)
			{
				cachedObject = fetch();
				isCached = true;
			}
			return cachedObject;
		};
	}
}