File tree Expand file tree Collapse file tree 1 file changed +5
-1
lines changed
Expand file tree Collapse file tree 1 file changed +5
-1
lines changed Original file line number Diff line number Diff line change @@ -192,13 +192,15 @@ If you need to cache an instance of something based on type, then you can store
192192public class Factory
193193{
194194 public T Create <T >()
195+ where T : new ()
195196 {
196197 return Cache <T >.Instance ;
197198 }
198199
199200 private static class Cache < T >
201+ where T : new ()
200202 {
201- public static readonly T Instance = new T ();
203+ public static readonly T Instance = new ();
202204 }
203205}
204206```
@@ -209,11 +211,13 @@ You can use the JIT to cache instances on your behalf instead of a slower `Concu
209211public class Factory
210212{
211213 public T Create <T >()
214+ where T : new ()
212215 {
213216 return Cache <T >.Instance ;
214217 }
215218
216219 private static class Cache < T >
220+ where T : new ()
217221 {
218222 public static readonly T Instance = CallExpensiveMethodToBuildANewInstance ();
219223
You can’t perform that action at this time.
0 commit comments