File tree Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Expand file tree Collapse file tree 1 file changed +6
-6
lines changed Original file line number Diff line number Diff line change @@ -186,7 +186,7 @@ public class MyDependencyFactory
186
186
187
187
## Caching singletons in generic types
188
188
189
- If you need to cache an instance of something based on type , then you can store it on the field of a generic type .
189
+ If you need to cache an instance of something based on type , then you can store it on a static field of a generic type .
190
190
191
191
```C #
192
192
public class Factory
@@ -196,9 +196,9 @@ public class Factory
196
196
return Cache <T >.Instance ;
197
197
}
198
198
199
- private class Cache < T >
199
+ private static class Cache < T >
200
200
{
201
- public T Instance = new T ();
201
+ public static T Instance = new T ();
202
202
}
203
203
}
204
204
```
@@ -213,11 +213,11 @@ public class Factory
213
213
return Cache <T >.Instance ;
214
214
}
215
215
216
- private class Cache < T >
216
+ private static class Cache < T >
217
217
{
218
- public T Instance = CallExpensiveMethodToBuildANewInstance ();
218
+ public static T Instance = CallExpensiveMethodToBuildANewInstance ();
219
219
220
- public T CallExpensiveMethodToBuildANewInstance ()
220
+ public static T CallExpensiveMethodToBuildANewInstance ()
221
221
{
222
222
// Imagine a really complex process to construct T
223
223
return instance .
You can’t perform that action at this time.
0 commit comments