@@ -20,14 +20,15 @@ protected ObjectPoolBase(Func<T> factory, Action<T> recycler, Action<T> disposer
2020 protected int MaxSize ;
2121 public int Capacity => MaxSize ;
2222
23+ /// <inheritdoc />
2324 /// <summary>
2425 /// Total number of items in the pool.
2526 /// </summary>
2627 public abstract int Count { get ; }
27- protected virtual int PocketCount => Pocket . Value == null ? 0 : 1 ;
28+ protected int PocketCount => Pocket . Value == null ? 0 : 1 ;
2829
29- protected Action < T > Recycler ; // Before entering the pool.
30- protected Action < T > OnDiscarded ; // When not able to be used.
30+ protected readonly Action < T > Recycler ; // Before entering the pool.
31+ protected readonly Action < T > OnDiscarded ; // When not able to be used.
3132
3233
3334 // Read-only because if Take() is called after disposal, this still facilitates returing an object.
@@ -36,14 +37,15 @@ protected ObjectPoolBase(Func<T> factory, Action<T> recycler, Action<T> disposer
3637
3738 public T Generate ( ) => Factory ( ) ;
3839
40+ // ReSharper disable once UnassignedField.Global
3941 protected ReferenceContainer < T > Pocket ; // Default struct constructs itself.
4042
4143 #region Receive (.Give(T item))
4244 protected virtual bool CanReceive => true ; // A default of true is acceptable, enabling the Recieve method to do the actual deciding.
4345
4446 protected bool PrepareToReceive ( T item )
4547 {
46- if ( item != null && CanReceive )
48+ if ( item != null && CanReceive )
4749 {
4850 var r = Recycler ;
4951 if ( r != null )
@@ -97,7 +99,7 @@ public bool TryTake(out T item)
9799 protected virtual bool SaveToPocket ( T item )
98100 => Pocket . TrySave ( item ) ;
99101
100- protected virtual T TakeFromPocket ( )
102+ protected T TakeFromPocket ( )
101103 => Pocket . TryRetrieve ( ) ;
102104
103105
@@ -122,11 +124,9 @@ protected override void OnBeforeDispose()
122124
123125 protected override void OnDispose ( bool calledExplicitly )
124126 {
125- if ( calledExplicitly && OnDiscarded != null )
126- {
127- T d ;
128- while ( ( d = TryRelease ( ) ) != null ) OnDiscarded ( d ) ;
129- }
127+ if ( ! calledExplicitly || OnDiscarded == null ) return ;
128+ T d ;
129+ while ( ( d = TryRelease ( ) ) != null ) OnDiscarded ( d ) ;
130130 }
131131 }
132132}
0 commit comments