@@ -914,22 +914,63 @@ static void ConsoleWrite(ReadOnlySpan<char> readOnlySpan)
914914 }
915915 #endregion
916916
917- #region SLazy<T>
917+ #region SLazy<T> + ValueLazy<T>
918918 {
919919 Console . WriteLine ( " SLazy<T>----------------------" ) ;
920920 Console . WriteLine ( ) ;
921- Console . WriteLine ( @" SLazy<T> is a struct alternative to Lazy<T> with some" ) ;
922- Console . WriteLine ( @" minor differences to: ToString, Equals, GetHashCode, and" ) ;
923- Console . WriteLine ( @" default constructor. There are benchmarks included in" ) ;
924- Console . WriteLine ( @" Towel's documentation." ) ;
921+ Console . WriteLine ( @" SLazy<T> is a faster Lazy<T> when using the default" ) ;
922+ Console . WriteLine ( @" LazyThreadSafetyMode.ExecutionAndPublication setting." ) ;
925923 Console . WriteLine ( ) ;
926924
927925 SLazy < string > slazy = new ( ( ) => "hello world" ) ;
928926
929- Console . WriteLine ( @$ " SLazy<string> slazy = new(() => ""hello world"");") ;
930- Console . WriteLine ( @$ " slazy.IsValueCreated: { slazy . IsValueCreated } ") ;
931- Console . WriteLine ( @$ " slazy.Value: { slazy . Value } ") ;
932- Console . WriteLine ( @$ " slazy.IsValueCreated: { slazy . IsValueCreated } ") ;
927+ Console . WriteLine ( @$ " SLazy<string> slazy = new(() => ""hello world"");") ;
928+ Console . WriteLine ( @$ " slazy.IsValueCreated: { slazy . IsValueCreated } ") ;
929+ Console . WriteLine ( @$ " slazy.Value: { slazy . Value } ") ;
930+ Console . WriteLine ( @$ " slazy.IsValueCreated: { slazy . IsValueCreated } ") ;
931+ Console . WriteLine ( ) ;
932+
933+ Console . WriteLine ( @$ " ValueLazy<T> is even faster than SLazy<T> but it") ;
934+ Console . WriteLine ( @$ " is unsafe as it will potentially call the factory") ;
935+ Console . WriteLine ( @$ " delegate multiple times if the struct is copied.") ;
936+ Console . WriteLine ( @$ " So please use ValueLazy<T> with caution.") ;
937+ Console . WriteLine ( ) ;
938+
939+ ValueLazy < string > valueLazy = new ( ( ) => "hello world" ) ;
940+
941+ Console . WriteLine ( @$ " ValueLazy<string> valueLazy = new(() => ""hello world"");") ;
942+ Console . WriteLine ( @$ " valueLazy.IsValueCreated: { valueLazy . IsValueCreated } ") ;
943+ Console . WriteLine ( @$ " valueLazy.Value: { valueLazy . Value } ") ;
944+ Console . WriteLine ( @$ " valueLazy.IsValueCreated: { valueLazy . IsValueCreated } ") ;
945+ Console . WriteLine ( ) ;
946+
947+ Console . WriteLine ( @$ " Here is the main different between SLazy<T> and ValueLazy<T>:") ;
948+ Console . WriteLine ( ) ;
949+
950+ int sLazyCount = 0 ;
951+ SLazy < int > sLazy1 = new ( ( ) => ++ sLazyCount ) ;
952+ SLazy < int > sLazy2 = sLazy1 ;
953+ Console . WriteLine ( @$ " int sLazyCount = 0;") ;
954+ Console . WriteLine ( @$ " SLazy<int> sLazy1 = new(() => ++sLazyCount);") ;
955+ Console . WriteLine ( @$ " SLazy<int> sLazy2 = sLazy1;") ;
956+ Console . WriteLine ( @$ " Console.WriteLine(sLazy1.Value); -> { sLazy1 . Value } ") ;
957+ Console . WriteLine ( @$ " Console.WriteLine(sLazy2.Value); -> { sLazy2 . Value } ") ;
958+ Console . WriteLine ( ) ;
959+
960+ int valueLazyCount = 0 ;
961+ ValueLazy < int > valueLazy1 = new ( ( ) => ++ valueLazyCount ) ;
962+ ValueLazy < int > valueLazy2 = valueLazy1 ;
963+ Console . WriteLine ( @$ " int valueLazyCount = 0;") ;
964+ Console . WriteLine ( @$ " ValueLazy<int> valueLazy1 = new(() => ++valueLazyCount);") ;
965+ Console . WriteLine ( @$ " ValueLazy<int> valueLazy2 = valueLazy1;") ;
966+ Console . WriteLine ( @$ " Console.WriteLine(valueLazy1.Value); -> { valueLazy1 . Value } ") ;
967+ Console . WriteLine ( @$ " Console.WriteLine(valueLazy2.Value); -> { valueLazy2 . Value } ") ;
968+ Console . WriteLine ( ) ;
969+
970+ Console . WriteLine ( @$ " Because the ValueLazy<T> was copied, it called the factory delegate") ;
971+ Console . WriteLine ( @$ " multiple times. That is why SLazy<T> is safe to use but you need to") ;
972+ Console . WriteLine ( @$ " be careful when using ValueLazy<T>.") ;
973+
933974 Pause ( ) ;
934975 }
935976 #endregion
0 commit comments