Skip to content

Commit a752c45

Browse files
committed
Make Cache<T>.Instance static
The `Instance` property of `Cache<T>` should be static in order to access the property by its type.
1 parent b52374c commit a752c45

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

1.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public class MyDependencyFactory
184184

185185
## Caching singletons in generic types
186186

187-
If you need to cache an instance of something based on type, then you can store it on the field of a generic type.
187+
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.
188188

189189
```C#
190190
public class Factory
@@ -194,9 +194,9 @@ public class Factory
194194
return Cache<T>.Instance;
195195
}
196196

197-
private class Cache<T>
197+
private static class Cache<T>
198198
{
199-
public T Instance = new T();
199+
public static T Instance = new T();
200200
}
201201
}
202202
```
@@ -211,11 +211,11 @@ public class Factory
211211
return Cache<T>.Instance;
212212
}
213213

214-
private class Cache<T>
214+
private static class Cache<T>
215215
{
216-
public T Instance = CallExpensiveMethodToBuildANewInstance();
216+
public static T Instance = CallExpensiveMethodToBuildANewInstance();
217217

218-
public T CallExpensiveMethodToBuildANewInstance()
218+
public static T CallExpensiveMethodToBuildANewInstance()
219219
{
220220
// Imagine a really complex process to construct T
221221
return instance.

0 commit comments

Comments
 (0)