logo SERENITY
Game Systems Foundation
Search Results for

    Show / Hide Table of Contents

    Class OnceGuard<TKey>

    Lets a piece of work happen exactly once per key. The canonical use is a warn-once latch: a misconfiguration that would otherwise log on every dispatch, every frame or every call gets reported the first time and stays quiet afterwards, while a DIFFERENT key still reports.

    Inheritance
    object
    OnceGuard<TKey>
    Inherited Members
    object.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.ReferenceEquals(object, object)
    object.GetHashCode()
    object.GetType()
    Namespace: Serenity.Shared.Utils
    Assembly: Serenity.Shared.dll
    Syntax
    public sealed class OnceGuard<TKey>
    Type Parameters
    Name Description
    TKey

    The key work is deduplicated by.

    Constructors

    OnceGuard()

    Creates a guard that compares keys with the type's default equality.

    Declaration
    public OnceGuard()

    OnceGuard(IEqualityComparer<TKey>)

    Creates a guard that compares keys with an explicit comparer.

    Declaration
    public OnceGuard(IEqualityComparer<TKey> comparer)
    Parameters
    Type Name Description
    IEqualityComparer<TKey> comparer

    The comparer used to decide whether two keys are the same.

    Methods

    HasEntered(TKey)

    Whether the key has already been entered.

    Declaration
    public bool HasEntered(TKey key)
    Parameters
    Type Name Description
    TKey key

    The key to test.

    Returns
    Type Description
    bool

    Reset()

    Forgets every key, so the guarded work can happen once more per key.

    Declaration
    public void Reset()

    TryEnter(TKey)

    Reports whether this is the first time the key is seen, and records it.

    Declaration
    public bool TryEnter(TKey key)
    Parameters
    Type Name Description
    TKey key

    The key to enter.

    Returns
    Type Description
    bool

    True the first time a key is passed; false on every later call with that key.

    In this article
    © 2026 Serenity. All Rights Reserved