Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp4
- {
- class Program
- {
- public class LazyItem
- {
- public readonly string foobar;
- public LazyItem(string foobar)
- {
- this.foobar = foobar;
- }
- }
- public class LazyContainer
- {
- private readonly Lazy<LazyItem> common;
- public LazyContainer()
- {
- common = new Lazy<LazyItem>(() => new LazyItem("common"));
- }
- public LazyItem Common => common.Value;
- }
- // https://msdn.microsoft.com/en-us/library/dd642331(v=vs.110).aspx
- static void Main(string[] args)
- {
- var c1 = new LazyContainer();
- Assert(c1.Common, c1.Common); // ok
- var c2 = new LazyContainer();
- Assert(c1.Common, c2.Common); // not ok
- }
- public static void Assert(object o, object o2)
- {
- if (o.GetHashCode() != o2.GetHashCode())
- {
- throw new Exception();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment