Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 8c4695f

Browse files
authored
Tweak some annotations on EventRegistrationTokenTable<T> (#25386)
1 parent 394edf4 commit 8c4695f

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/System.Private.CoreLib/src/System/Runtime/InteropServices/WindowsRuntime/EventRegistrationTokenTable.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public sealed class EventRegistrationTokenTable<T> where T : class
1818

1919
// Cached multicast delegate which will invoke all of the currently registered delegates. This
2020
// will be accessed frequently in common coding paterns, so we don't want to calculate it repeatedly.
21-
[AllowNull, MaybeNull] private volatile T m_invokeList = null!; // TODO-NULLABLE: Remove ! when nullable attributes are respected
21+
private volatile T? m_invokeList = null;
2222

2323
public EventRegistrationTokenTable()
2424
{
@@ -32,8 +32,7 @@ public EventRegistrationTokenTable()
3232

3333
// The InvocationList property provides access to a delegate which will invoke every registered event handler
3434
// in this table. If the property is set, the new value will replace any existing token registrations.
35-
[MaybeNull]
36-
public T InvocationList
35+
public T? InvocationList
3736
{
3837
get
3938
{
@@ -45,7 +44,7 @@ public T InvocationList
4544
{
4645
// The value being set replaces any of the existing values
4746
m_tokens.Clear();
48-
m_invokeList = null!; // TODO-NULLABLE: Remove ! when nullable attributes are respected
47+
m_invokeList = null;
4948

5049
if (value != null)
5150
{
@@ -55,7 +54,7 @@ public T InvocationList
5554
}
5655
}
5756

58-
public EventRegistrationToken AddEventHandler(T handler)
57+
public EventRegistrationToken AddEventHandler(T? handler)
5958
{
6059
// Windows Runtime allows null handlers. Assign those a token value of 0 for easy identity
6160
if (handler == null)
@@ -85,7 +84,7 @@ private EventRegistrationToken AddEventHandlerNoLock(T handler)
8584
// Update the current invocation list to include the newly added delegate
8685
Delegate? invokeList = (Delegate?)(object?)m_invokeList;
8786
invokeList = MulticastDelegate.Combine(invokeList, (Delegate)(object)handler);
88-
m_invokeList = (T)(object?)invokeList!;
87+
m_invokeList = (T?)(object?)invokeList;
8988

9089
return token;
9190
}
@@ -180,7 +179,7 @@ public void RemoveEventHandler(EventRegistrationToken token)
180179
}
181180
}
182181

183-
public void RemoveEventHandler(T handler)
182+
public void RemoveEventHandler(T? handler)
184183
{
185184
// To match the Windows Runtime behaivor when adding a null handler, removing one is a no-op
186185
if (handler == null)
@@ -196,8 +195,7 @@ public void RemoveEventHandler(T handler)
196195
// value. Therefore we need to make sure we really have the handler we want before taking the
197196
// fast path.
198197
EventRegistrationToken preferredToken = GetPreferredToken(handler);
199-
T? registeredHandler;
200-
if (m_tokens.TryGetValue(preferredToken, out registeredHandler))
198+
if (m_tokens.TryGetValue(preferredToken, out T? registeredHandler))
201199
{
202200
if (registeredHandler == handler)
203201
{
@@ -228,15 +226,14 @@ public void RemoveEventHandler(T handler)
228226

229227
private void RemoveEventHandlerNoLock(EventRegistrationToken token)
230228
{
231-
T? handler;
232-
if (m_tokens.TryGetValue(token, out handler))
229+
if (m_tokens.TryGetValue(token, out T? handler))
233230
{
234231
m_tokens.Remove(token);
235232

236233
// Update the current invocation list to remove the delegate
237234
Delegate? invokeList = (Delegate?)(object?)m_invokeList;
238235
invokeList = MulticastDelegate.Remove(invokeList, (Delegate?)(object?)handler);
239-
m_invokeList = (T)(object?)invokeList!;
236+
m_invokeList = (T?)(object?)invokeList;
240237
}
241238
}
242239

0 commit comments

Comments
 (0)